• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Returning Objects...........

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
below is a small program which i've lifted from a book (Java 2: The Complete reference, by H. Schildt).



The output generated form the program is as follows:-

ob1.a: 2
ob2.a: 12
ob2.a after second increase: 22


:roll:

But i have some queries regarding this.

In the line Test ob1 = new Test(2);

If you follow this, surely the first line of the output should be 12, and not 2, i.e., ob1.a: 12

Just how exactly is this program working??

Sory for asking such an obvious question, but i'm racking my brains over this.

Cheers in advance
 
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The new function will internall invoke the constructor of the Test object, and the constructor is setting the instanc variable named 'a'.

If you are still getting confused, try putting System.out messages in the constructor and incrByTen function. That should give you an idea in what order the functions are being called
 
Steve Jensen
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Still don't understand why

ob1.a: is 2

 
Jayesh Lalwani
Ranch Hand
Posts: 502
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, let's try step by step

Test ob1 = new Test(2);// at this point ob1.a = 2 and ob2 = null

ob2 = ob1.incrByTen(); //this calls the incrByTen function; which boils down to this

Test temp = new Test(ob1.a + 10); // at this point ob1.a = 2 and temp.a = 12
ob2 = temp;//s= ob1.a = 2 and ob2.a = 12


See, incrByTen is not changing the value of a in ob1; it's just creating a new object who's a is 10 more than ob1.a
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If you follow this, surely the first line of the output should be 12, and not 2, i.e., ob1.a: 12



The reason that it is not 12 is because this method:



Is creating it's own object and return that. It is not returning Ob1 or any member of Ob1, it is simply invoking the incrByTen() method found in the Ob1 object.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason a=2 the first time is because when you create the object ("new") you by default call the constructor. Variables inside the constructor live only for the life of the constructor, then they go bye-bye. Also, the variable "a" in the constructor is not the same as the the instance variable "a". A lot of people use "_a" for instance variable names.

When you passed the number (2), you passed it into the constructor and it set it to "2".

When you did "ob2 = ob1.incrByTen();" the value of ob1.a was already 2, so you added 10 to it and it made it 12. Get it?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic