constructor in java

In the programming of Java and C language, object initialization must be performed on the created object. Java uses the constructor introduced by C++ to initialize the object, which is a special method that is automatically called when an object is created.

Note: The name of the constructor must be exactly the same as the class name

class Constructor{                                //cover from Mr.Summer
Constructor(){  //this is a constructor.
  System.out.println("Constructor");}
}

public class Text{
    public static void main(String[] args){
        for(int i=0;i<3;i++)
            new Constructor();}
}/*Output: Constructor Constructor Constructor*/

When creating an object:

new Constructor( );

Storage space will be automatically allocated for the object and the corresponding constructor will be called, which ensures that it has been initialized before use.


There are two types of constructors:

① A constructor without any parameters, that is, the default constructor;

②Constructor with parameters, as follows:

class Constructor2{
Constructor2(int i){
    System.out.println("Constructor "+i+" ");  }
}

public class Text2{
    public static void main(String[] args){
        for(int i=0;i<3;i++)
            new Constructor2(i);}
}/*Output:  Constructor 0 Constructor 1 Constructor 2*/


Constructor overloading:

It has already been stated that the name of the constructor must be the same as the name of the class. What if you want to create an object in multiple ways? Suppose you create a class that can be initialized in the standard way, and can be initialized by reading the required information from a file, which requires two constructors: a default constructor, and a constructor with variables. This requires overloading of the constructor.

The following example demonstrates both an overloaded constructor and an overloaded method:

class Tree{                                        //cover from Thinking in Java
    int height;
    Tree(){
        System.out.println("Planting a seedling");
        height = 0;}

    Tree(int i){
        height = i;
        System.out.println("Creating new Tree that is " + height + " feet tall");}

    void info(){
        System.out.println("Tree is " + height + " feet tall");}
    
    void info(String s){
        System.out.println(s+":Tree is " + height + " feet tall");}

}

public class Test{
    public static void main(String[] args){
        for(int i = 0 ; i < 3 ; i++) {
            Tree t = new Tree(i);
            t.info();
            t.info("overloaded method");}
        new Tree();
}
}/*Output
Creating new Tree that is 0 feet tall
Tree is 0 feet tall
overloaded method:Tree is 0 feet tall
Creating new Tree that is 1 feet tall
Tree is 1 feet tall
overloaded method:Tree is 1 feet tall
Creating new Tree that is 2 feet tall
Tree is 2 feet tall
overloaded method:Tree is 2 feet tall
Planting a seedling*/

When creating a Tree object, it can either contain no parameters or use the height of the tree as a parameter. The former represents a sapling, and the latter represents a tree that already has a certain height. To support this creation, there must be a default constructor and a constructor that takes a height as a parameter.


Overloaded constructors can also be distinguished by parameters. The rules are simple, because in Java, any overloaded method must have a unique parameter type list, for example:

Tree(int, string) and Tree(string, int) are two different overloaded constructors


the usage of this in the constructor

public class Money{
    int i = 0;
    String s = "hello";
    
Money(int i){
    System.out.println("you have "+i+" dollar.");}

Money(String ss){
    System.out.println(ss+" ,nice to meet you.");
    s = ss;}

Money(String s,int i){
    this(i);
    this.s=s;
    System.out.println("go Money(String s,int i)");}

Money(){
    this("hi" , 10);
    System.out.println("go Money( )");}

public static void main(String[] args){
    Money m = new Money();}

}/*Output
you have 10 dollar.
Go Money(String s, int i)
Go Money( )*/

Create an object in the main function, the object first calls the Money() function, because this( " hi " , " 10 " ) in the Money() function is a constructor that points to the type of Money(s , i ), in this constructor There is also a constructor that points to type Money(i), so Money(i) is executed first, then Money(s , i ), and finally Money().

The usage of this here is to clearly point to a constructor, which is convenient for calling


The order of initialization is static (static) objects first, then non-static objects, just initialization, not static functions!







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324415684&siteId=291194637