• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Object usage

 
Greenhorn
Posts: 17
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test{
int a;
Test(int i){
a=i;
}
Test incrByTen(){
Test temp=new Test(a+10);
return temp;
}
}
class RetOb{
public static void main(String args[]){
Test ob1=new Test(2);
Test ob2;

ob2=ob1.incrByTen();
System.out.println("ob1.a: "+ob1.a);
System.out.println("ob2.a: "+ob2.a);

ob2=ob2.incrByTen();
System.out.println("ob2.a after second increase: "+ob2.a);
}
}

Output

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


If objects have to be allocated first using 'new' before any attempt to use them in the program. Then how do these lines of code work

ob2=ob1.inceByTen();

ob2=ob2.inceByTen();

as ob2 hasn't been allocated at all.

Can anybody please explain this
 
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If objects have to be allocated first using 'new' before any attempt to use them in the program. Then how do these lines of code work
ob2=ob1.inceByTen();
ob2=ob2.inceByTen();
as ob2 hasn't been allocated at all.


Your incrByTen() method creates an instance of the Test class and returns it so it's assigned to the reference "ob2". Hence you can use any methods from the object referred by "ob2:

And please UseCodeTags when posting code. Unformatted code makes it hard to read the post.

 
natt walters
Greenhorn
Posts: 17
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much.
Sorry for the inconvenience. Sorry to ask but how do I do usecodetags while posting a query. I'm using my iPhone at the moment and don't have access to a PC for now. I do apologise if I should have known it by now.
 
Vijitha Kumara
Bartender
Posts: 4109
72
Hibernate Fedora Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No worries . I haven't use the mobile site for posting but I'm sure you can wrap your code around [code][/code] tags for Java code.
 
natt walters
Greenhorn
Posts: 17
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thank you very much
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic