-java Base-Constructor

Source: Internet
Author: User

1. Constructors

A constructor is the initialization method of a class-creation object. Its name is the same as the class name. Assuming that no constructor is written, the virtual machine automatically adds a constructor with no parameters, and if "Learn Java, go to the Gothic Academy Kaige123.com" is written with a constructor, then the VM will not be added to the constructor.

 Public  classTest1  Public  Test1()  {System.out.println ("I am the builder! ");}}
2. Constructor overloading

A constructor is a method, so it can do the same method overloading as a normal method, in other words, a constructor overload. Constructor overloading, like method overloading, is more than one constructor with a different number of name parameter types and parameters.

 Public class Test1{Public   Test1(){System. out. println ("Take off your clothes first.");}Public   Test1(String name){}Public   Test1(int name){}}

2. The instance block instance block is a partitioned code area. Code 1. It is executed before the constructor. 2. It executes with property initialization. 3. There can be a number of instance blocks in a class.

    1. Execution is performed in order from top to bottom.
 Public class Test3 {System.} out. println ("I am the instance block! ");}}

3.this this is used in 3 ways: 1. Represents the current object. Assuming that the parameter passed in a method is named A1, there is also a variable in the instance called A1. If the parameters in the method A1 want to be assigned to A1 in the class. Then you need to go to the This keyword. Because Java will default to your nearest parameter if it encounters a variable with the same name.

 public  class  Test4 {String name; //the name of the rule used is the nearest principle  public  test4    { this . Name = name;}} 2.  the current object returns public  Test4 gettest4   ()   {return  this ;} 

3. Constructor forwarding

Note: What about static properties and static method parameters? You cannot use this at this time. Because this is the current object. Since it is a static member, it is possible to directly name the class name attribute. For example:

Static String Static void Method (StringTest4. Address = address;}

4. Static Block static{} is executed when the class is loaded with a static property. are executed sequentially from top to bottom.

5. Loading of Classes 1. When declaring a static property but not using an attribute, the class is not loaded. The class is loaded only when a static property is used. 2. When a constant is declared or used, the class is not loaded. Because the constant memory "learns Java, to the Kaige123.com Academy" and the class member's memory is not in the same memory. 3. Using a static method or new will definitely load the class. 4.//Load Class Class.forName (package name. Class name)

Strange question:

The main method, new Test1 (), but the Test1 class is also new Test1, because the new object will certainly load the class, but the load class is actually loaded static properties, so this is a dead loop. The report method nested call overflow error. Java.lang.StackOverflowError

-java Base-Constructor

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.