This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

class construction

 
Ritesh raushan
Ranch Hand
Posts: 101
  • 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
Pie
Posts: 49382
62
  • 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: 101
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks brother and sorry for short question....
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic