Forum : Creating new instance
Brief description  about Online courses   join in Online courses
View Padmini  Manjunath 's Profile

Creating new instance

class MyDetails
{
public static void main(String[] args)
{
//Creating an instance of Details class
Details info1=new Details();
Details info2=new Details();

//Name and age passed to methods of first person
info1.setName("Padmini");
info1.setAge(20);
info1.print();

//Name and age passed to methods of second person
info2.setName("Manjunath");
info2.setAge(25);
info2.print();
}
}
Here above, cant we use the same instance info1 instead of info2 for calling method setAge and print to display second person details? I checked it,works fine, but does it make any differrence.
Asked by Padmini Manjunath | Apr 15, 2010 |  Reply now
Replies (2)
View avinash pagunta 's Profile
its one and the same,those are just two different objects created in heap area of the jvm architecture
Apr 24, 2010
View arun kumar das 's Profile
Hi Padmini,

Yes, you are right. We can use the same instance since both info1 and info2 are of type Details. These can be used to get different values from the setage method purely based on the arguments they would be passing. In the baove example you can also use info1.setAge(20) and info1.setAge(25).

Cheers,
Kamal
Apr 15, 2010