Java Constructor (Note 11)

Java constructor

1. Constructor

Features:
//There are multiple constructors, which one matches when the new object is called, which one is called
1) Executed when the class object is created, and only once
2) same as class name
3) No return value, not even void
public class Japan {
void Japan() { //This is not a constructor, although it compiles without error
}
}
4) cannot be called manually
5) Can be overloaded, not rewritten
6) If a class is designed without a constructor, it will generate a default, no-parameter empty constructor
7) If you define a constructor yourself, the system will no longer generate a default constructor
8) The main function is to initialize the class object
//for example
public class Japan {
Japan(){ //system default
System.out.println("The constructor with no arguments was called");
}
Japan(String msg){
System.out.println("This is what my master told me to say: "+msg);
}
int age;
String name;
String country;
void speak(){
System.out.println("Hokkaido is Chinese territory");
}
}

class Test{
public static void main(String[] args) {
Japan japan=new Japan(); //call the constructor without parameters
Japan japan2=new Japan("This is the incoming parameter"); //call the constructor with parameters
}
}
 
//Example, use the constructor to pass parameters
// Solve the initialization problem of parameters through the constructor
public class TV {
private int width;
private int height;
private String pinpai; //brand
private String color; //color
TV(int width,int height,String pinpai,String color){
this.width=width; //this points to the class object, the front this.width refers to the width in the class object, the latter width is a parameter, this acts as a pointer to distinguish
this.height=height;
this.pinpai=pinpai;
this.color=color;
}
void turnOn () {
System.out.println(pinpai+"brand TV is on");
}
void turnOff(){
System.out.println("The TV is smoking, the smoke is "+color+" color");
}
void play(){
System.out.println("Martial Arts");
}
void show(){
System.out.println("This TV:");
System.out.println("体积:" +width*height);
System.out.println("Brand:" +pinpai);
System.out.println("颜色:" +color);
}
}

class Test2{
public static void main(String[] args) {
TV tv=new TV(200,150,"Panda","Silver Grey");
tv.show();
tv.turnOn ();
tv.play();
tv.turnOff();
}
}

2. this keyword

this can call properties and methods of this class
E.g:
TV(String pinpai,String color){
this.pinpai=pinpai;   //this points to the class object, the front this.pinpai refers to the pinpai in the class object, the latter pinpai is a parameter, this plays a similar role as a pointer to distinguish
this.color=color; //Use this to call the properties of this class
this.turnOn(); //Use this to call the method of this class
....
}
  
//Use this (parameter list) to call another constructor of this class in a constructor
TV(int width,int height,String pinpai,String color){
this(pinpai, color);  //Use this to call other constructors, and it must be placed in the first sentence. The calling format is this plus parentheses to pass parameters directly
this.width=width;
this.height=height;
}

3. Objects are passed as parameters

1) Basic data types as function parameters
public static void main(String[] args) {
int score=59;
change(score);
System.out.println(score); // 59, you can see that for basic data types, as parameters of functions, changes to formal parameters will not affect actual parameters, because it does not share heap memory objects, it is two stack memory
}

static void change(int score){ //The formal parameter of the function can be regarded as the most local change 
score=90;
}

2) The reference type is used as the parameter of the object 
public class Test3 {
public static void main(String[] args) {
Student stu=new Student();
stu.score=59;
change(stu);
System.out.println(stu.score); //90   
}
static void change(Student stu){  //When the reference type is used as the parameter of the function, the change to the formal parameter will affect the actual parameter, because the heap memory object is shared, changing the heap memory will affect..
stu.score=90;
}
}

class Student{
int score;
}

Fourth, the static keyword

static means static
//Example 1 requirement: Count how many instances of the Cat class are created in total
public class Cat {
Cat(){
count++;
}
static int count; //Used to count how many instances of this class are created
int age;
String name;
}

class Test5{
public static void main(String[] args) {
Cat c1=new Cat();
Cat c2=new Cat();
Cat c3=new Cat();
Cat c4=new Cat();
System.out.println(c4.count);  //4
System.out.println(c1.count);  //4
}
}
Characteristics of the static keyword
1) Multiple objects, sharing the same data (attached: there is no global variable in java, when there is such a need, you can use the static keyword to achieve)
2) The members it modifies are loaded when the class is loaded, and initialized, only once
3) It can modify properties or methods, and the members modified by it can use the class name directly. (Recommended) Reference can also use the object name. References, static members, can exist without objects
4) The non-static context cannot be accessed from the static context (in a static method, the this keyword is not possible)
//The example is in an instance method, accessing a static member
public class Cat {
static String home="中国";
int age;
String name;
//instance method
void talk(){
System.out.println("My home is: "+home); //In the instance method, access static members, OK
System.out.println("My age: "+age);
System.out.println("my name"+name);
}
}

class Test5{
public static void main(String[] args) {
Cat c=new Cat();
c.talk();
}
}

//Example, in a static context, the non-banded context (context: function, or field) cannot be accessed
class Cat {
static String home="中国";
int age;
String name;

//instance method
void talk(){
System.out.println("My home is: "+home); //ok in the instance method, access static members, OK
System.out.println("My age: "+age);
System.out.println("my name"+name);
}
    
// Attempt to access non-static members in static methods, error
static void speak(){
System.out.println("My name is: "+age); //Error Cannot make a static reference to the non-static field name
}
}
     
     附: public static void main(String[] args) 
     -- public indicates that the function is public
      -- static indicates that the function is static
      -- void means this function has no return value
      -- main is a fixed name
      -- String[] indicates that the function receives an array type parameter
      --args parameter name
   
Example of passing parameters to the main function:
class Test{
public static void main(String [] args){
for(int i=0;i<args.length;i++){
System.out.println(args[i]); //Take out the parameters passed to the main function
}
}
}
When running, java Test parameter 1 parameter 2 ....

5. Constructing code blocks and static code blocks

//example 
class Sheep{
{ //Construction code block, executed earlier than the constructor, executed every time the object is created
System.out.println("Construct code block");
}
Sheep(){ //Constructor
System.out.println("Constructor");
}
static {
System.out.println("This is a static code block");
}
}

//example
class Test6{
public static void main(String[] args) {
Sheep sheep1=new Sheep();
Sheep sheep2=new Sheep();
}

//this is the static code block
//construct code block
//Constructor

//construct code block
//Constructor
}


// The output of the program below the example
public static void main(String[] args) {
new Person(9999);
new Person();
}
    
class Person{
Person(){
System.out.println("a");
}
Person(int x){
System.out.println(x);
}
static{ // static members are only initialized once
System.out.println("b");
}
{
System.out.println("c");
}
}
//b c 9999 c a

6. Loading and Execution Order of Classes

Cat cat=new Cat(); 
 
  1) First load the bytecode file Cat.class into memory
  2) Static members are initialized first (static properties, static code blocks)
  3) Open up space on the heap memory, and class members are initialized by default
  4) Explicit initialization of class members
  5) Construct code block execution
  6) Constructor execution
  7) Point the reference in stack memory to the entity in heap memory


    //The following two static members, who initializes first? Whoever writes it in front, who initializes first
    class cat{
    int age=90;
    int weight=15;
    String nickName="tom";
    static String home="Zhao Mingming's home" //Static property
    static { //static code block
    System.out.println("喵~~");
    }
    }

Guess you like

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