File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes class construction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "class construction" Watch "class construction" New topic
Author

class construction

Ritesh raushan
Ranch Hand

Joined: Aug 29, 2012
Posts: 100
diff between..
new Horse(); and

Horse horse=new Horse();
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Aug 29, 2012
Posts: 100
thanks brother and sorry for short question....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
You’re welcome
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: class construction