final--
Final is used for a class to indicate that the class cannot derive a subclass.
Final is used to indicate that the method cannot be overridden by a quilt class.
Final is used as a variable to represent a constant, similar to the const keyword of C + +.
Final is used for member variables to indicate that the member variable is a constant, cannot be modified, and must be assigned when the variable is defined.
Final is used for local variables to indicate that the local variable is a constant, cannot be modified, can be assigned when the variable is defined, or it can be assigned after the variable is defined.
static--
Static is used for a member variable to indicate that there is only one copy of the variable, that is, the static member variable belongs to the class and not to a specific class instance object, and all class instance objects share the static member variable, and the static member variable is completed by the corresponding class name. Static member variables can be initialized or unassigned when defined, and are automatically initialized when not assigned, remembering that local variables cannot be static.
Static is used for methods so that a static method can be invoked through the class name without instantiating the class, and it is important to note that the This keyword cannot be used within a static method, that non-static methods cannot be invoked, and that non-static member variables cannot be referenced.
Static is used for a class, which refers to an inner class, so that the static inner class can be referenced elsewhere by the external class name.
Static can also be used for code blocks of classes, called Static code block, is in the class independent of the class members of the static statement block, can have multiple, the location can be casually put, it is not in any method body, the JVM loading classes will execute these static code blocks, if the static code block has more than one, The JVM executes them sequentially in the order in which they appear in the class, and each block of code is executed only once.
Access rights for classes (y/n)
|| Keywords | | Class | | Package | | Sub Class | | Other Packages | |
| Public | Y | Y | Y | Y |
| protected | Y | Y | Y | N |
| Default | Y | Y | N | N |
| Private | Y | N | N | N |
Public, protected, and private three keywords, which can be used for classes (inner classes), member variables, and member functions, the default access rights are valid in packages, and the minimum access rights are selected when the class access permission is not the same as the access rights of the member variable or member function.
interface/implements/extends/class--
Interface is used to declare an interface, the methods in the interface are declared, not implemented, the access rights can be public or default permissions, and can be specified as abstract.
Implements is used to implement interfaces, all the methods in the interface need to be implemented, and multiple interfaces can be implemented simultaneously.
Extends is used to inherit a parent class or parent interface, which can only be a single inheritance when inheriting a parent class, and interface inheritance supports multiple inheritance, unlike multiple inheritance in C + +.
Class is used to declare classes, access rights can be public or default permissions, and can be specified as abstract, final, and access rights are different for top-level classes and inner classes.
abstract--
Abstract is used for classes to represent this class as an abstract class and cannot be instantiated.
Abstract is used to represent this method as an abstract method, simply declaring, not implementing, subclasses, and abstract methods that cannot use private and static keywords.
Here are some highlights of this and super use:
The
this
java keyword This is available only in the method method body. When an object is created, the Java Virtual Machine (JVM) assigns the object A pointer to itself, the name of this pointer. Therefore, this can only be used in Non-static methods in a class, and this is absolutely not allowed in static methods and static blocks of code, which is clearly explained in the "Java keyword static, final usage Summary" article. And this is only associated with a particular object, not the class, and different objects of the same class have different this. A comprehensive example using this is given below to illustrate the problem:
Package org.leizhimin;
public class Test6 {private int number;
Private String username;
private String password;
private int x = 100;
public Test6 (int n) {number = N;//This can also be written as: this.number=n;
Public Test6 (int i, string username, string password) {//member variables and parameters with the same name, member variables are masked, and member variables are accessed in the form of "this. Member variable".
This.username = Username;
This.password = password; }//default constructor with no parameters public Test6 () {This (0, "unknown", "null");//Call another constructed method} public Test6 (String name) {This (1, name, "Empty");
Another constructor is invoked through this: public static void Main (String args[]) {Test6 T1 = new Test6 ();
Test6 t2 = new Test6 ("Tourist");
T1.outinfo (t1);
T2.outinfo (T2);
private void Outinfo (Test6 t) {System.out.println ("-----------");
System.out.println (T.number);
System.out.println (T.username);
System.out.println (T.password); f ();
This can be written as: This.f ();
The private void F () {//local variable has the same name as the member variable, the member variable is masked, and the member variable is accessed in the form of "this. Member variable".
int x;
x = this.x++;
SYSTEM.OUT.PRINTLN (x);
System.out.println (this.x); }//Return to current instanceThe reference to the private Test6 getself () {return this;
}
}
The results of the operation are as follows:
-----------
0
Unknown
air
-----------
0
visitors
101
&NBSP
Look at the example above to see if you want to use this:
first, invoke another construct method through this, with the hair is this (argument list), this is only in the class construction method, nowhere else.
Second, function arguments, or local variables and member variables in a function with the same name, the member variable is masked, and you need to access the member variable with "this. Member variable name" To refer to the member variable. Of course, in the absence of the same name, you can directly use the name of the member variable, without this, use is not wrong, hehe.
Third, in a function, you need to refer to the current object of the class to which the letter belongs, using this directly.
In fact, these usage summaries come from a more in-depth understanding of the phrase "This is a pointer to the object itself", rote otherwise easy to forget and easily mistaken, to understand!
Super
Super Key and this function are similar, is a masked member variable or member method that becomes visible, or is used to refer to a masked member variable and member member method.
However, super is used in subclasses to access the masked members in the immediate parent class, and the immediate parent class (the closest superclass above the class) is noted. Here is an example of a comprehensive use of super, there are two classes: A Father class, a subclass of the Father class son, through these two classes fully demonstrated the use of super, then the code:
Package org.leizhimin;
public class Father {public String v= "Father";
The public String x= "outputs the public member variable x!!! of the Father class";
Public Father () {System.out.println ("Father constructed method is invoked!");
} public Father (String v) {this.v= "Father class with parameter construction Method!";}
public void Outinfo () {System.out.println ("Father Outinfo method called");
The public static void main (string[] args) {//TODO automatically generates method stubs}} package org.leizhimin;
The public class Son extends father{the public String v= "Son"; Public Son () {super ();
The constructor for the superclass is called, and can only be placed in the first row.
System.out.println ("Son constructor method is called!"); Super ();
Wrong, it must be placed at the front of the construction method body.
Public Son (String str) {super (str);
System.out.println ("Son with parameter construction method is called!");
}//Overridden Superclass member Method Outinfo () public void Outinfo () {System.out.println ("son Outinfo () method called"); public void Test () {String v= "haha!";
The local variable v covers the member variable v and the superclass variable v System.out.println ("------1-----"); System.out.println (v); Output local variable v System.out.println (THIS.V); Output (subclass) member variable v System.out.println (SUPER.V); Output Super class member variable v SYSTEM.OUT.PRINTLN ("------2-----"); SYSTEM.OUT.PRINTLN (x); Output superclass member variable V, subclass inherits to System.out.println (Super.x);
Output Super class member variable v System.out.println ("------3-----"); Outinfo (); Call the Outinfo () method of the subclass This.outinfo (); Call the Outinfo () method of the subclass Super.outinfo ();
Call the parent class's Outinfo () method} public static void Main (string[] args) {new Son (). Test ();
}
}
Sub-class son run result:
Father constructor method is called!
Son constructor method is called!
------1-----
haha!
Son
Father
------2-----
Output The public member variable x!!!
of the Father class The public member variable x!!!
of the Father class is exported The Outinfo () method of the------3-----Son is called
Son the Outinfo () method is invoked
father the Outinfo method is called