Java Common keywords (static, final, this, super)

Source: Internet
Author: User

this keyword

The This keyword has the function of

1. Calling member variables of this class

 Public class soft{         privateint  A;           Public Soft (int  a) {             this. a=A;      }} 

2. Call the constructor of this class, but put it in the first row

  1  public  class   soft{  2   3
     public   Soft () {  4   5  this  (" Hello "  6  }   7   8  public   Soft (stirng name) {  9  10  }  11  12 } 

3. Call the Member method in this class, but it is not necessary, if used, must be true love.

4. The most practical one, can be used as a reference to the current object

 PackageCom.yqg.day3;/** The This keyword references to its own objects*/ Public classKeyword_this {intI=0;  PublicKeyword_this (inti) { This. i=i; }         Publickeyword_this Leef () {i++; return  This; }         Public voidprint () {System.out.println ("I=" +i); }}

--------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------

2.static keywords

Variables that are modified by static are shared by all objects of the class and initialized when used for the first time.

3 Public classCat {4 5/**6 * Static member variable 7*/8Private Static intSid = 0; 9 10PrivateString name;11 12intID;13 14Cat (String name) {15 This. Name =name;id = sid++;17     }18 19 Public voidinfo () {System.out.println ("My name is" + name + ", NO." +ID);21st     }22 23 Public Static voidMain (string[] args) {CAT.SID = 100;Cat Mimi =NewCat ("Mimi");Cat Pipi =NewCat ("Pipi");27mimi.info ();28pipi.info ();29     }30}

There are two methods of accessing static member variables

I. Any object can access this static value, except that it accesses the data in the same piece of memory, and the value of the statically decorated member variable is stored in the data area the first time it is used.

Two. Accessing static variables by class name

External reference Link: https://www.cnblogs.com/xdp-gacl/p/3637407.html

Static can also modify methods, but methods that are modified by static can only inherit and cannot be overridden. Because the static modifier is shared, unique. Overrides, however, embody polymorphism, and the uniqueness of static conflicts.

Static methods cannot access non-static variables or methods, non-static members and methods are owned by different objects of the class, and static is common to all objects, and the two conflict with each other.

Static can also be used to modify the statement, the main initialization work, the class is loaded when the start is executed, the execution order is from top to bottom all the static statement execution, from top to bottom to execute all the new.

Static can also decorate the inner class, which mainly modifies the members and methods inside the class.

---------------------------------------------------------------------------------------------------------

3.final keywords

Final, immutable.

Final is bound in the compile phase, called a static binding.

Final can modify the method, the modified method cannot be overwritten, the private method is final decorated by default.

The final decorated class, the decorated class cannot be inherited, cannot have subclasses, all methods in the final class are the final method by default, and the members can not be fianl decorated.

Final decorated member variables, the final variable must be assigned, the value of the base reference type cannot be modified, the object that references the execution cannot be modified, but the contents of the object can be modified.

Note that classes or methods cannot be abstractor and final decorated at the same time, and Abstractor classes and methods must be extended and rewritten.

--------------------------------------------------------------------------------------------------------------- ---------------------

Super keyword

Super represents a reference to the parent class, which refers to the parent class object.

When a class has a parent class, the child class object is instantiated while the parent class object is instantiated, and if the subclass overrides the method of the parent class or has the same member variable as the parent class, in order to distinguish between the two, a super refers to the parent class object when calling the parent class method and member variable.

classFatherclass {9 Public intvalue;10 Public voidf () {value=100;System.out.println ("The Value property of the Parent class =" +value);13     }14 }15 16/**17 * Subclass ChildClass inherit from parent class Fatherclass *@authorgacl19 *20*/21stclassChildClassextendsFatherclass {22/**23 * Subclass In addition to inheriting the Valu attribute that the parent class has, it declares a value property, 24 * That is, the subclass at this time has two value properties. -*/26 Public intvalue;27/**28 * In subclass ChildClass, the implementation of the F () method inherited from the parent class is rewritten, that is, the method body of the F () method is rewritten. in*/30 Public voidf () {31Super. f ();//use Super as the reference object of the parent class object to invoke the F () method inside the parent class objectvalue=200;//This value is the Valu defined by the subclass, not the value inherited from the parent class .System.out.println ("value of child class =" +value);System.out.println (value);//The value of that value, which is a subclass custom, is printed .35/**36 * The value is printed in the parent class, since the subclass is overriding the F () method inherited from the parent class, 37 * The first sentence "Super.f ();" is to have the reference object of the parent class call the F () method of the parent class object, 38 * that is, the parent object calls the F () method itself to change the value of its Value property, from 0 to 100. 39 * So the value printed here is 100. +*/System.out.println (Super. value);42     }43 }44 45/**46 * Test class *@authorgacl48 *49*/50 Public classTestinherit {51 Public Static voidMain (string[] args) {ChildClass cc =NewChildClass ();53cc.f ();54     }55}

Analyzing any program starts with the first sentence of the main method, so first analyze the first sentence in the Main method:

Chlidclass cc = new Chlidclass ();

When the program executes here, first in the stack space will produce a variable cc,cc inside the value is what this is not good to say, in a word, through this value we can find the new Chlidclass object. Because subclass Chlidclass is inherited from the parent class Fatherclass, when we new a subclass object, the subclass object contains a parent object, and the parent object has its own property value. This value member variable is declared in the Fatherclass class and does not initialize it, so the system defaults to initialize it to 0, the member variable (declared within the class) can not initialize it when declaring, the compiler will automatically initialize this member variable, But a local variable (declared inside a method) must be initialized at the time of declaration, because the compiler does not automatically initialize the local variable, and any variable must be initialized before it is used.

Subclasses inherit the Value property of the parent class at the same time, they also define a value property individually, so when we new a subclass object, this object will have two value properties, one is inherited from the parent class value, and the other is its own value. The member variable defined in the subclass value is not initialized to it at the time of declaration, so the compiler defaults to initialize it to 0. Therefore, after executing the first sentence, the layout of the system memory is as follows:

1 cc.f ();

When the new object comes out, the object produces a reference to this, which points to the object itself. If the new object is a subclass object, then there is a super reference inside the subclass object, which points to the parent object inside the current object. So the equivalent of a program inside a this,this point to the object itself, there is a super,super point to the current object inside the parent object.

Here is called after overriding the F () method, the first sentence of the method body: "Super.f ();" is to let the parent object inside the subclass object call its own f () method to change the value of its Value property, and the parent object calls his own F () method by pointing to his reference super, so after executing this sentence, the value of values inside the parent object becomes 100. Then execute "value=200;" The vaule here is the value that the subclass object declares itself, not the value inherited from the parent class. So after this sentence has been executed, the subclass object itself value is changed to 200. The memory layout at this point is as follows:

The last three sentences in the body of the method are the commands to print the value values, and the first two lines are printed with the value of the subclass object itself, so the printed result is 200, and the last Word prints the parent object's own value of the subclass object, and prints out the result of 100.

In this case, the entire memory analysis is over, and the final memory display results as shown above.

Java Common keywords (static, final, this, super)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.