If we initialize an array of a custom class MyClass.java like this MyClass[] myClass = new MyClass[5]; then how can we instantiate member variable of that class through its setters methods? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

If we initialize an array of a custom class MyClass.java like this MyClass[] myClass = new MyClass[5]; then how can we instantiate member variable of that class through its setters methods?

e.g to instantiate a "name" data member variable of MyClass.java, how can we instantiate this "name" variable.

12th Jul 2016, 1:02 PM
Saeed Ahmed Khan
Saeed Ahmed Khan - avatar
2 Answers
0
myClass[0] = new MyClass (arg); and so on...
12th Jul 2016, 1:26 PM
Gershon Fosu
Gershon Fosu - avatar
- 1
Thanks for reply. But I forgot to mention that actually MyClass.java has a default constructor so it can't take any parameter while creating new instance of it. So, finally I have come to following solution: MyClass[] myClass = new MyClass[5]; myClass[i] = new MyClass(); myClass[i].setName("Person"); where i is the index of element of the array myClass that is being instantiated.
13th Jul 2016, 6:42 AM
Saeed Ahmed Khan
Saeed Ahmed Khan - avatar