Introduction to Java (Java Classes and Objects - Class Constructors)

First, the construction method of the class

    In addition to member methods in a class, there is also a special type of method, that is, the constructor. The constructor is a method with the same name as the class, and the creation of objects is done through the constructor. Whenever a class instantiates an object, the class automatically calls the constructor.

    The characteristics of the constructor: (1) the constructor has no return value; (2) the name of the constructor should be the same as the name of the class.

    When defining a constructor, the constructor has no return value, but this is different from the ordinary method without a return value. The ordinary method without a return value is defined in the form of public void methodEx(), but the constructor does not need to use the void key. Make modifications.

    grammar:

public book (){
    //....Constructor body
}

    public : constructor modifier

    book : the name of the constructor

    Member variables can be assigned values ​​in the constructor, so that when an object of this class is instantiated, the corresponding member variables will also be initialized.

    If no constructor is explicitly defined in the class, the compiler automatically creates a default constructor with no parameters.

    If none of the constructors defined in the class is a parameterless constructor, the compiler will not set a default parameterless constructor for the class. When trying to call the parameterless constructor to instantiate an object, the compiler will report an error . So only if the class does not define any constructor, the compiler will automatically create a constructor with no parameters in the class.

    eg : this can call the member methods and member variables of the class, as well as the constructor of the class.

public class AnyThting{
    public AnyThting(){ //Define a parameterless constructor
        this("this calls the parameterized constructor"); //Use this to call the parameterized constructor
        System.out.println("No parameter constructor");
    }
    public AnyThting(String name){ //Define a parameterized constructor
        System.out.println("Constructor with parameters");
    }
}

    You can see that two constructors are defined. In the parameterless constructor, you can use the this keyword to call the parameterized constructor. But in this way, it should be noted that you can only use this in the first sentence of the parameterless constructor to call the parameterized constructor.

Second, static variables, constants and methods

    A variable decorated with static. Constants and methods are called static variables, constants and methods.

    Sometimes, when working on a problem, you need two classes to share a piece of data in the same memory area. For example, if the constant PI is used in the ball class, it may be necessary to use this constant in another circle class in addition to this constant. In this case, there is no need to create PI constants in two classes at the same time, because then the system will allocate the two constants not defined in the same class to different memory spaces. To solve this problem, you can set this constant to be static, and the PI constant is shared in memory.

    Scrap variables, constants and methods declared static are called static members. Static members belong to the class and are different from individual objects. Static members can be called in a class or other classes using the class name and the " . " operator.

    Syntax: classname.static class member

    eg : Create a class, call the static member in the main method of the class and output it in the console.

public class StaticTest{
    static double PI =3.1415; //Define static constants in the class
    static int id; //define static variable in class
    public static void method1(){ //define static method in class
        // doSomething
    }
    public void method2(){
        System.out.println(StaticTest.PI); //Call static constants
        System.out.println(StaticTest.id); //Call static variables
        StaticTest.method1(); //Call the static method
    }
}

    Set up 3 static members, namely constants, variables and methods, and then call these 3 static members in the method2() method, which can be called directly using the form of "class name.static member".

    While static members can also be called using the "object.staticMember" form, this is generally not recommended because it tends to confuse static and non-static members.

    The role of static data and static methods is usually to provide shared data or methods, such as mathematical calculation formulas, etc., which are declared and implemented as static, so that when needed, these static members can be called directly using the class name. Although it is convenient to call static members this way, static members are also subject to the constraints of the public , private and protected modifiers.

    eg : Create a class, call the static member in the main method of the class and output it in the console.

public class StaticTest{
    static double PI =3.1415; //Define static constants in the class
    static int id; //define static variable in class
    public static void method1(){ //define static method in class
        // doSomething
    }
    public void method2(){ //Define a non-static method in the class
    System.out.println(StaticTest.PI); //Call static constants
     System.out.println(StaticTest.id); //Call static variables
        StaticTest.method1(); //Call the static method
    }
    public static StaticTest method3(){ //Define a static method in the class
        method2(); //call non-static method
        return this; //Use the this keyword in the return statement
    }
}

    After entering the above code in eclipse, the compiler will report an error, because the method3() method is a static method, and the non-static method and this keyword are called in its method body. There are two rules for static methods in the Java language:

    (1) The this keyword cannot be used in static methods.

    (2) Non-static methods cannot be called directly in static methods.

    In Java, it is stipulated that the local variables in the method body cannot be declared as static.

public class example{
    public void method(){
        static int i= 0; // this is the wrong input
    }
}       

    If you want to perform the initialization of the class first, you can use static to define a static area.

public class example{
    static{
        //some
    }
}

    When this code is executed, the program in the static block is executed first and only once.

Third, the main method of the class

    The main method is the entry point of the class, which defines where the program starts; the main method provides control over the flow of the program, and the Java compiler executes the program through the main method.

    grammar: 

public static void main(String[] args){
        // method body
}

    The main method has the following characteristics:

    (1) The main method is static, so if you want to use other methods directly in the main method, the method must also be static.

    (2) The main method has no return value.

    (3) The formal parameter of the main method is an array. The other args[0]~args[n] represent the first parameter to the nth parameter of the program respectively. You can use args.length to get the number of parameters.

    eg : Create a class, write code in the main method, and set program parameters.

public class TestMain{
    public static void main(String[] args){ //Define the main method
        for(int i = 0 ; i < args.length ; i++){ //loop according to the number of parameters
            System.out.println(args[i]); //loop to print parameter content
        }
    }
}

    Suppose you set 3 parameters to it: parameter 1, parameter 2, parameter 3 

    Then the running result is:

parameter 1
parameter 2
parameter 3



Guess you like

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