A method that is executed when a class is instantiated, and is mainly used to initialize member variables of that class.
The constructor is a special method that is called when an instance is created, and it is possible to specify arguments in the same way as a normal method.
The call to the constructor is described as follows.
public class main class name{
public static void main(String[] args) {
//Instant generation
Class name variable name=new class name();
}
}
class class name{
//constructor(Executed when instantiating)
public class name(){
Initialization process, etc.
}
}
The constructor uses the new operator, and the method with the same class name is executed only once when the instance is created.
A constructor is a process that is executed when you create an instance of a class.
For example, if you want to create an instance of a class called SampleClass, code as follows.
SampleClass instanceA = new SampleClass();
The constructor is called by SampleClass (), which is written after new in this syntax.
Recommended Posts