+ 2
How I can instance a constructor??
constructor in java
3 Answers
+ 3
We create instance of a CLASS and define a constructor in Java. I think you might have mixed up those termsđ
.
Anyways, define a constructor as follows :-
public class A{
int a, b;
A(){
} //a default constructor
A(int n1, int n2){
a = n1;
b = n2;
} //a parameterized constructor
}
You can define a constructor in any of the above ways. As for creating an instance of the class,
A aObj = new A();
A object2 = new A(6, 5);
In this way you can create any number of objects of class A you want.
I hope this answers your question âș
+ 3
Soo Make an Object?
myObject name = new myObject();
// will call default constructor
myObject name = new myObject(5);
// will call constructor with one int parameter
@Nihar In my defence, I have no defence đ±
+ 1
That's true, @Any Question? since Java creates a default constructor for us if we don't have one âș