Java programming constructors and object creation in detail

  • 2020-11-20 06:04:23
  • OfStack

java constructor and object creation

An object can be declared with a class and must be created after the object is declared

1 Construction method

First of all, let's talk about what a constructor is, since we've already said that this is a constructor, then it's obviously essentially a constructor.

So, since it's a method, it should look something like a method. It calls back 1 Class(); Since then, I haven't seen any other code that defines methods. This is because, in the absence of a custom constructor for a class, the compiler automatically adds a default constructor to it at compile time

(1) When a program creates objects with a class, it needs to use the constructor of the class

(2) The constructor name in the class must be exactly the same as the class name, and there is no type

(3) It is allowed to write several constructors in one class, but the parameters must be different (the number of parameters is the same, but the corresponding type of a parameter in the parameter list is different; Different number of parameters)

(4) If no constructor is written in the class, the system defaults to only one constructor for the class (no arguments, no statements in the method body).

1.1. Default constructor and custom constructor

Java does not provide a default constructor if one or more constructors are defined in the class

1.2. Constructors have no type

2 Create objects

2.1. Declaration of the object

Class name object name


// Example: 
Person person ; 

2.2. Assign variables to declared objects

Use the constructor method of the new operator and class to assign variables to declared objects, that is, create objects


// Example: Assign variables to declared objects 
public class Example4_2_Point {
  int x;
  int y;
  Example4_2_Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
}
 
public class Example4_2 {
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    Example4_2_Point example4_2_Point1 = new Example4_2_Point(10, 10);// Declare an object and assign variables to the object to use new And the constructor in the class 
    Example4_2_Point example4_2_Point2 = new Example4_2_Point(23, 25);// Declare an object and assign variables to the object to use new And the constructor in the class 
  }
}

2.3. Memory model of the object

2.4 Object of Use

"·" operator: Access to your variables and method calls can be achieved by using the "·" operator

1. The object operates on its own variables (representing the properties of the object)

(1) Access to its own variables and method invocation can be achieved by using the "·" operator

(2) Point operators, also known as reference operators or access operators, are formatted as: object · method

3. An object invokes a method in a class (representing the behavior of the object)

3.1 References and entities of objects (omitted)

Code examples:


// Default constructor, custom constructor, non-constructor example 
class Lader(){
	int x ,y;
	// methods 1 : The default constructor 
	Lader(){
	}
	// methods 2 : Custom constructor 
	Lader(){
		x = 1;
		y = 1;
	}
	// methods 3 : Custom constructor 
	Lader(int a,int b){
		x = a;
		y = b;
	}
	// methods 4 : The method type is void , so it is not a constructor 
	void Lader(int a,int b){
		x = a;
		y = b;
	}
	// methods 5 : The method type is int , so it is not a constructor 
	int Lader(int a,int b){
		x = a;
		y = b;
	}
}

conclusion

That's the end of this article on Java's programming constructors and object creation, and I hope you found it helpful. If you have any questions, please feel free to leave a message. This site will reply to you in time. We look forward to your valuable comments.


Related articles: