Summary of some key usage tips in Java

  • 2020-04-01 04:08:37
  • OfStack

Final -

Final for a class means that the class cannot be subclassed.

Final is used for a method to indicate that the method cannot be subclassed.

Final represents a constant when used with variables, similar to the const keyword for C/C++.

Final is used with a member variable to indicate that the member variable is constant, cannot be modified, and must be assigned when the variable is defined.

Final is used ina local variable to indicate that the local variable is a constant and cannot be modified. It can be assigned to a variable when it is defined or after it is defined.

The 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 a class but not to a specific class instance object. All class instance objects share the static member variable, and the corresponding class name is used to access the static member variable. Static member variables can be initialized or not at the time of definition, and are automatically initialized when they are not. Remember that local variables cannot be static.

Static is used for methods, making it possible to call static methods by class name without instantiating the class. It is important to note that this keyword cannot be used in static methods, non-static methods can not be called, and non-static member variables can not be referenced.

Static is used for classes, in this case inner classes, so you can refer to the static inner class elsewhere by the outer class name.

Static class can also be used for a block of code is called a static block, is independent of the members of the class in the class the static blocks, and you can have multiple, position can be literally put, it is not in any way in the body, the JVM will perform these static loading class code block, if there are multiple static code block, the JVM will order in which they appear in the class of execute them in sequence, each block of code will be executed only once.

Class access (Y/N)

|| keyword || class || package || subclass || other packages |0 |1
| public | Y | Y | Y | Y |
| protected | Y | Y | Y | N |
| default | Y | Y | N | N |
| private | Y | N | N | N |

The three keywords public, protected and private can be used for class (inner class), member variable and member function. The default access is valid in the package. When the access of the class is different from that of the member variable or member function, select the lowest access.

Interface/implements/extends/class --

Interface is used to declare an interface. Methods in an interface are declared but not implemented. Access can be public or default, and can also be specified as abstract.

Implements is used to implement an interface, you need to implement all the methods in the interface, and you can implement multiple interfaces at once.

Extends is used to inherit from a parent class or a parent interface, which can only be inherited from a single class. Unlike C++ with multiple inheritance, interface inheritance supports multiple inheritance.

Class is used to declare classes, access can be public or default, can also be specified as abstract, final, access to the top-level class and inner class restrictions are different.


Abstract -

Abstract is used by a class to indicate that the class is abstract and cannot be instantiated.

Abstract is used for a method to indicate that the method is an abstract method, which only needs to be declared, not implemented, but implemented by a subclass. Abstract methods cannot use the private and static keywords.

Let's focus on the use of this and super:

this
              The Java keyword this can only be used within the method body. When an object is created, the Java virtual machine (JVM) assigns the object a pointer to itself, the name of which is this. Therefore, this can only be used in non-static methods in the class, and it can never appear in static methods and static code blocks, which is clearly explained in the article "Java keyword static, final use summary". And this is only associated with specific objects, not with classes. Different objects of the same class have different this. Here is a comprehensive example of using this 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: this.number=n;
 }
 public Test6(int i, String username, String password) {
 //The member variable has the same name as the parameter, the member variable is masked, and the member variable is accessed by "this.member variable".
 this.username = username;
 this.password = password;
 }
 //Default constructor with no arguments
 public Test6() {
 this(0, " The unknown ", " empty "); //This calls another constructor method
 }
 public Test6(String name) {
 this(1, name, " empty "); //This calls another constructor method
 }
 public static void main(String args[]) {
 Test6 t1 = new Test6();
 Test6 t2 = new Test6(" tourists ");
 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();
 }
 private void f() {
 //The local variable has the same name as the member variable, the member variable is shielded, and the member variable is accessed in the way of "this.member variable".
 int x;
 x = this.x++;
 System.out.println(x);
 System.out.println(this.x);
 }
 
 //Returns a reference to the current instance
 private Test6 getSelf() {
 return this; 
 }
}

 
The operation results are as follows:


-----------
0
 The unknown 
 empty 
100
101
-----------
0
 tourists 
 empty 
100
101

 
              Look at the above example and explain when this is needed:
              First, call another constructor with this, this(argument list), which is only used in the constructor of the class.
              Second, when the function parameter or the local variable in the function has the same name as the member variable, the member variable is shielded. In this case, to access the member variable, the member variable needs to be referred to in the way of "this.member variable name". Of course, in the absence of the same name, you can directly use the name of the member variable, but not this, used it is not wrong, hehe.
              Third, in a function, when you need to refer to the current object of the class to which the function belongs, use this directly.
              In fact, these usage summary are from the "this is a pointer to the object itself" sentence of a deeper understanding, remember or easy to forget and easy to mistake, to understand!
 
super
 
              Super key is similar to this in that it is used to make the masked member variables or methods visible, or to refer to the masked member variables and methods.
However, super is used in subclasses to access the masked members of the direct superclass, which is the nearest superclass above the class. The following is an example of combining super. There are two classes: a Father class and a subclass of Father class, Son. These two classes fully demonstrate the use of super.
 


package org.leizhimin;
public class Father {
 public String v="Father";
 public String x=" The output Father Of the class public Member variables x!!!";
 
 public Father() {
 System.out.println("Father The constructor is called !");
 }
 
 public Father(String v){
 this.v="Father Class with a parameter constructor ! Run the .";
 }
 public void outinfo(){
 System.out.println("Father the outinfo Method is called ");
 } 
 public static void main(String[] args) {
 //TODO automatically generates method stubs
 }
}
 
package org.leizhimin;
public class Son extends Father{
 public String v="Son";
 
 public Son() {
 super();   //Calls the constructor of the superclass and can only be placed on the first line.
 System.out.println("Son The no-argument constructor is called !");
 //super();   // The wrong , Must be placed in front of the constructor body .
 } 
 
 public Son(String str){
 super(str);
 System.out.println("Son The constructor with parameters is called !");
 }
 //Overrides the superclass member method outinfo()
 public void outinfo(){ 
 System.out.println("Son the outinfo() Method is called ");
 } 
 
 public void test(){
 
 String v=" Ha ha ha ha !";  //The local variable v overrides the member variable v and the superclass variable v
 
 System.out.println("------1-----");
 System.out.println(v);  //Output the local variable v
 System.out.println(this.v); //Output (subclass) member variable v
 System.out.println(super.v); //Output the superclass member variable v
 
 System.out.println("------2-----");
 System.out.println(x);  //Output superclass member variable v, from which subclasses inherit
 System.out.println(super.x); //Output the superclass 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 outinfo() method of the parent class
 } 
 
 public static void main(String[] args) {
 new Son().test();
 
 }
}

 
Running results of subclass Son:
 


Father The constructor is called !
Son The no-argument constructor is called !
------1-----
 Ha ha ha ha !
Son
Father
------2-----
 The output Father Of the class public Member variables x!!!
 The output Father Of the class public Member variables x!!!
------3-----
Son the outinfo() Method is called 
Son the outinfo() Method is called 
Father the outinfo Method is called 


Related articles: