How I can instance a constructor?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How I can instance a constructor??

constructor in java

6th Jul 2017, 11:08 PM
Luis Felipe De La Rosa Garcia
Luis Felipe De La Rosa Garcia - avatar
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 ☺
7th Jul 2017, 1:01 AM
Nihar Raote
Nihar Raote - avatar
+ 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 😱
7th Jul 2017, 12:39 AM
Rrestoring faith
Rrestoring faith - avatar
+ 1
That's true, @Any Question? since Java creates a default constructor for us if we don't have one ☺
7th Jul 2017, 2:52 AM
Nihar Raote
Nihar Raote - avatar