Java (11) builder

Source: Internet
Author: User

In Java, the constructor is called by the New keyword to be created in memory.

Use the constructor to initialize the object, and in this way you can allocate memory for the object and do other initialization work and other operations

Name of Class modifier class (parameter list)

{

Method body

}

Public, protected, default, private

Cannot have return value

Package constructor Demo;public class Apple {private String s;private Apple () {System.out.println ("constructor Run");} public static Apple Gettest () {return new Apple ();} Public String GetS () {return s;} public void Sets (String s) {THIS.S = s;}} The package constructor Demo;public class Testapple{public static void Main (String args[]) {Apple a = Apple.gettest (); A.sets (" Assigning a value to a private member variable "); String s = a.gets (); System.out.println (s);}}

My late father, class, sub-class constructor

A constructor with a parameter that has a default parameterless constructor does not perform the

public class Apple extends fruit

{

Public Apple ()

{

Call the constructor method of the parent class

Super ();

}

}

List mode two objects point to the same object.

Loads the parent class to create the object, as well as member variables and other inheritance relationships.

Loads the static and static member variables of the class, initializes it, and so on.

When static and static member variables are loaded, the object is created and the non-static member variable is loaded, initialized, and so on

Executes the body of the method in the constructor, after which the object of the class is created.

The order in which the parent class runs is the same as the order of the class

Parent class Static Statement

Sub-class static statements

Parent class constructor

Sub-class constructors

Common methods

Java (11) builder

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.