Use final, static, this, super's

final keyword

final keyword is mainly used in three places: variables, methods and classes.

  1. For a final variable, if the basic data types of variables, then its value can not be changed once after the initialization; if it is a reference type variable, then after its initialization will not allow its point to another object.

  2. When modifying a class with a final, indicating that this class can not be inherited. All members of the final class methods will be implicitly designated as the final method.

  3. There are two reasons for using final approach. The first reason is the method of locking to prevent any derived class to modify its meaning; second reason is efficiency. In earlier versions of Java implementation, it will be built into final method call. However, if the method is too large, you may not see any performance embedded calls to bring (the current version of Java do not need to use these methods to optimize the final). All private class methods implicitly designated as final.

The static keyword

There are four main static keyword usage scenarios:

  1. Member variables and member methods modifications: the modified static members belong to the class, not part of a single class of objects, is shared by all objects in the class, and can be recommended by the class name calling. Static member variables are declared in a static member variables, static variables stored in the memory area of Java method area. Call format:类名.静态变量名 类名.静态方法名()
  2. Static block of code: static block of code is defined in an outer class methods, static block of code (static block of code -> non-static block -> Constructor) before the non-static block. No matter how many objects the class is created, static code block is executed only once.
  3. Static inner class (static modification of that kind of thing can only be modified inside the class): there is a maximum difference between static inner class with non-static inner classes: non-static inner classes after the completion of the translation will implicitly holds a reference that points to create its enclosing class, but static inner classes do not. Without this reference means that: 1. It does not require the creation of dependence created enclosing class. 2. It can not use any non-static member variables and methods of the enclosing class.
  4. Static guide package (used to import class static resources, new features after 1.5): The format is: import staticThese two keywords can be used in conjunction with a designated import the specified static resource class, and does not require the use of the class name to call the class static member, you can use the class static member variables and member methods directly.

this keyword

this keyword is used to reference the current instance of the class. E.g:

class Manager {
    Employees[] employees;
     
    void manageEmployees() {
        int totalEmp = this.employees.length;
        System.out.println("Total employees: " + totalEmp);
        this.report();
    }
     
    void report() { }
}

In the example above, this keyword is used in two places:

  • this.employees.length: access variables in the current instance of the class Manager.
  • this.report (): method call to the current instance of the class Manager.

This keyword is optional, which means that if the same performance in the above example without using this keyword. However, using this keyword may make the code more readable or understandable.

super keyword

super keyword is used to access variables and methods from the parent class is a subclass. E.g:

public class Super {
    protected int number;
     
    protected showNumber() {
        System.out.println("number = " + number);
    }
}
 
public class Sub extends Super {
    void bar() {
        super.number = 10;
        super.showNumber();
    }
}

In the example above, Sub class to access the parent class member variable number and call its parent class's Super showNumber()methods.

Use this super problem and to note:

  • When you call the other parent class constructor in the super, when it is called on the first line of the constructor! When this call other constructor in this class, but also on the first line.
  • this, super not be used in a static method.

Simple explanation:

Modified static members belong to the class, not part of a single class of objects, is the class of all objects share. And this representative of a reference to this class of objects, directed present class object; and super representative parent class object reference to point to the parent object; therefore, this and super something belonging to the target areas, the static method is what belongs to the class category .

reference

Guess you like

Origin www.cnblogs.com/scholar-hwg/p/12172422.html