Java constructs a method, the package reference, the Final,super and so on the word summary

Source: Internet
Author: User

I. Construction method: When a class is instantiated, the first thing to do when it is new is the constructor, in the Java class, if the declaration constructor is not displayed, the JVM gives the class a default constructor. A class can have more than one constructor. The main function of a constructor is to instantiate the class. The second is what to do when the class is instantiated, and which properties to initialize. When a class declares a constructor, the JVM will no longer assign a default constructor to that class. The constructor is actually used to instantiate a class and a variable. Two. Package reference, package reference There are two ways one is a common import, the second is to precede the class with packagename.classname in fact, when the Java compiler class if the class in a Java package is automatically compiled into a package class name. A well-known package does not have access to the class of the nameless package (because the local class will default to the class in its own package if it does not have a package name), the class in the nameless package can access the class in the well-known package (the class in the Nameless package and the package under a directory) three. Final

Final   the decorated class is not allowed to be inherited.

  A class cannot be both final and abstract. Because the primary purpose of abstract is to define a convention that allows subclasses to implement such conventions, and final indicates that the class cannot be inherited, and that the two contradictions.

Final Modification MethodThe final decoration method, which indicates that the method cannot overwrite the override with a method in the Quilt class .

Final method
Declaring the method as final means that you already know that the functionality provided by this method satisfies your requirements, does not need to be extended, and does not allow any class inheriting from this class to overwrite the method, but inheritance can still inherit this method, which means that it can be used directly. There is also a mechanism called inline, which allows you to insert the method body directly into the call when you call the final method, instead of making routine method calls, such as saving breakpoints, pressing stacks, and so on, which may improve your program efficiency, but when your method body is very large, Or you call this method in many places, then your calling body code will quickly expand, it may affect efficiency, so you should be careful to use final method definition.

Final class
When you use final for a class, you need to think carefully, because a final class cannot be inherited by anyone, which means that the class is a leaf class in an inheritance tree, and that the design of such a class is considered perfect without the need for modification or extension. For members in the final class, you can either define it as final or not final. And for the method, because the class is the final relationship, Nature will become final type. You can also explicitly add a final to the method in the final class, but this is obviously meaningless.

final modifier variable

Final   A member variable represents a constant that can only be assigned once, and the value is no longer changed after the assignment.

When final modifies a native data type, the value representing the native data type cannot be changed;

If final modifies a reference type, it means that the reference type cannot point to another object, but the contents of the object to which the reference is pointing can change.

is essentially one thing, because the value of the reference is an address, the final requirement value, that is, the value of the address does not change.

Final modifies a member variable (property) that must be displayed for initialization.

  There are two types of initialization , one is initialized at the time of the variable declaration, and the second is to not assign the initial value when declaring the variable, but to assign an initial value to the variable in all the constructors of the class where the variable resides.

When the parameter type of a function is declared final, the parameter is read-only.

The use of super

Yesterday wrote this usage summary, suddenly produced a problem, consult others, have a little knowledge of their own. or write it down, for everyone to better understand to provide a little thought.

1) Someone has written a good initialization property constructor, and you just want to add some of your own new properties to the initialization, so that in one constructor to call another constructor, you can avoid duplication of code, reduce the amount of work;

2) When you call another constructor in one constructor, you should use the same memory space, initialize the variable in the default constructor, and override the value of the initialized variable when calling another.

3) The entire invocation of the process and recursive call function is a bit similar, keep inflating the ball, until the whole balloon expands, continuous deep progression, encounter stop mark, layer by hop out.

Wrote a piece of code explaining my narrative above:

1 class Javantiger {2     int age;      Age 3     int hight;    Body Height 4  5 public     Javantiger () {6         print (); 7         this.age=2;   The value of age is initialized here, but when it is returned recursively, this value is overwritten with 8     } 9 public     Javantiger (int.) {one         ());      Calling its own first constructor, the following two statements do not perform the number of         this.age = age;13         print ();     }15 public     javantiger (int, int hight) {         (age);   Call yourself the second constructor  , the number of two statements below does not perform the         this.hight = hight;19         print ();}21 public     void print () {  //Print function         System.out.println ("I am a" + Age + "Year" + hight + "ruler Gao tiger!");     }25 public     static void Main (string[] args) {         new Javantiger (3,3);}28}29//output30     //i ' am a 0 years 0 feet high tiger!31//i ' am a 3 years 0 feet tall tiger!32//i ' am a 3 year old 3 feet high tiger!  

Personal understanding is this, there may be problems, such as the constructor recursive call and the program function recursive invocation of the mechanism seems to be the same? The constructor is generated with the object, that is, the memory space is allocated at the same time, will the recursive call such as this will disrupt the order of allocating memory? Hope to see the Daniel to the final decision, give a best explanation.

Today to summarize is the use of super this keyword, super in the constructor is usually the time when the class inherits the other classes, super is to call the parent class's constructor, the paste segment code First

1 class Tiger {2     int ages;//age 3     int hight;//Body Height 4  5 public     Tiger () {6         print (); 7     } 8  9< c8/>public void print () {         System.out.println ("I am a" + Age + "Year" + hight + "ruler high tiger!"); One     }12}13 public class Javantiger extends Tiger {+-public     Javantiger () {"         super" ();    Call the parent class parameterless constructor     }17 public     static void Main (string[] args) {         new Javantiger ();     }20}

In fact, the super () in the constructor of class Javantiger can not write, Java will default to call the parent class parameterless constructor, but if the parent class does not define a parameterless constructor, there is no syntax error, the program will automatically exit, there is no print statement, At this point you need to manually call the constructor of the other parent class, paste the snippet code:

1 class Tiger {2     int ages;//age 3     int hight;//Body Height 4  5 public     Tiger (int. age) {6     this.age = aged; 7< C6/>print (); 8     } 9 public     void print () {         System.out.println ("I'm a" + Age + "old" + hight + "ruler high tiger!");     }12}13 public class Javantiger extends Tiger {+    public     Javantiger () {[         1];    Call the parent class constructor with parameters}17 public     static void Main (string[] args) {         new Javantiger ();     }20}

The super (1) in this code must be written in, or the compiler will make an error. So I simply summed up, "this () is calling its own other constructors, super () is called the constructor of the parent class of its own inheritance", if only to invoke the default parameterless parent class constructor, do not write in the subclass of the constructor function, but the actual programming, always ignore this point.

Can the two of these keywords appear in a constructor of a subclass at the same time? The answer must be no. Let's say your own understanding:

1) When creating a new base class, regardless of the recursive invocation of its own constructor, eventually you will call the parent class constructor, (not explicitly called, the system will call the default parameterless parent constructor);

2) in JAVA, the use of this and super must be placed in the first row of the constructor, only one first row; use only one otherwise dead loop

Reprint: http://www.cnblogs.com/java-class/archive/2012/12/20/2826499.html

Usage of this

When defining a variable with a class name, the definition should be just a reference, which can be used to access the properties and methods inside the class, which is enough to have a reference to access their properties and methods. Oh, Java provides a good thing, is the this object, it can be in the class to reference the properties and methods of the class. Let's start with a simple example:

Class Thisdemo {      String name= "Mick";    void Print (String name) {System.out.println (attribute Name= in class + this. Name); System.out.println ("property of local parameter =" +void main (string[] args) {Thisdemo tt=new Thisdemo (); Tt.print (" Orson "); }}

With reference to the return class itself, Thing in Java has a classic example of returning itself to this object by this keyword and then implementing multiple operations in a single statement, or sticking it out.

 public class    Thisdemo {int number; Thisdemo increment () {number++; return thisprivate void print () {System.out.println ("number=" +< Span style= "color: #000000;" >number); } public static void< Span style= "color: #000000;" > main (string[] args) {Thisdemo tt=new Thisdemo (); Tt.increment (). Increment (). Increment (). print (); }} 

That should also be defined in a class two constructors, in a constructor through this reference to invoke another constructor, which should be implemented, such implementation mechanism in the actual application development will have what kind of use? Put down the code:

PublicClassThisdemo {String name;IntAgePublicThisdemo () {This.age=21; }Public Thisdemo (String name,Int age) {this (); this.name= "Mick" ;} private void print () { System.out.println ("Final name =" +this.name "); System.out.println ("Final age =" +this.age);} public static voidnew Thisdemo ("", 0); // random arguments  Tt.print () ; }} 

Look at the above code, although it is very short, it seems logical to understand, in the constructor of the parameter is assigned to the name in the parameterless value of the age property. But I personally think that there is a problem, instantiating a class should first allocate memory for the object Thisdemo, first call the constructor Thisdemo (String name,int age), execute to the first row, call the Thisdemo () constructor, That means there should be two memory space, one is Thisdemo (String name,int age) execution to a point and the other is Thisdemo () execution space, Why did the final printout result in an instantiation of two properties in an object name and age? Please enlighten me!

To summarize:

1) The This keyword is a reference to itself within the class, and it is convenient for the method in the class to access its own properties;

2) You can return a reference to the object's own class, and you can call another constructor in one of the constructors (there is a problem in it)

Reprint: http://www.cnblogs.com/java-class/archive/2012/12/19/2825463.html

Java constructs a method, the package reference, the Final,super and so on the word summary

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.