This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class construction

 
Ritesh raushan
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
diff between..
new Horse(); and

Horse horse=new Horse();
 
harshvardhan ojha
Ranch Hand
Posts: 157
1
Android Java MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ritesh please ask questions properly, do that others can understand this.
new Horse(); and Horse horse=new Horse(); are same but new Horse(); here you have created object but you have no reference to it.
You cant use it. instead new Horse().method(); makes sense. But this object becomes eligible for garbage collection as it has no reference from a live thread. Hope this makes sense.
 
Campbell Ritchie
Sheriff
Posts: 48452
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does make sense, but may be difficult to understand for people not fluent in English.

new Horse() means create a Horse object. You haven’t done anything with it, but you can use it as foo(new Horse); or new Horse().foo()
   You can only use that object once, unless you can retrieve it from another object using it.
Horse h = new Horse() means create a Horse object and apply the name h to it. You can now use it as often as you like, as long as h is in scope and hasn’t been reassigned.
 
Ritesh raushan
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks brother and sorry for short question....
 
Campbell Ritchie
Sheriff
Posts: 48452
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic