Discussion board
Creating new instance
By Padmini Manjunath
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.
Reply
Post   Reset
Arun Kumar Das replied to Padmini Manjunath Thursday, April 15, 2010
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