wood burning stoves 2.0*
The moose likes Java in General and the fly likes Number of ways of creating object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Number of ways of creating object?" Watch "Number of ways of creating object?" New topic
Author

Number of ways of creating object?

Saral Saxena
Ranch Hand

Joined: Apr 22, 2011
Posts: 202

Hi Folks,
Please guide me what are the various ways of creating an object in java other than new operator and deserialization..?please explain me in detail..!1thanks in advance..!!
lokesh sree
Ranch Hand

Joined: Oct 27, 2009
Posts: 99

May be you can go through this http://stackoverflow.com/questions/95419/what-are-all-the-different-ways-to-create-an-object-in-java


Lokesh
( SCBCD 5, CCENT, SCJP 5 )
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Saral Saxena wrote:


What's that for? Seriously, this is an extremely unimportant question and not knowing the answer to it will not affect your skill as a Java programmer in the least.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

you can create an object by assignment.


SCJP
Visit my download page
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Randall Twede wrote:you can create an object by assignment.
Can you really?
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Campbell Ritchie wrote:
Randall Twede wrote:you can create an object by assignment.
Can you really?


Perhaps he has in mind something like
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

yeah, that's what i meant.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1526
Dennis Deems wrote:

Perhaps he has in mind something like


That way this should work too.


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 157

Randall Twede wrote:you can create an object by assignment.

Dennis Deems wrote:
Perhaps he has in mind something like



I'm not sure I agree that an object can be created by assignment. Looks at this;

An object will still be created, but not due to assignment. Perhaps objects for literals are created as soon as the line of code where the literal is declared is executed.


"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Riaan Nel wrote:An object will still be created, but not due to assignment. Perhaps objects for literals are created as soon as the line of code where the literal is declared is executed.

I think Dennis was pointing to say the object creation (Integer) happened since the literal was assigned to a wrapper reference. Else no object would have been created.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7554
    
  18

Akhilesh Trivedi wrote:
Dennis Deems wrote:Perhaps he has in mind something like
That way this should work too.

Actually, I suspect all you can really say is that both might create an object .

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
I would say that...is actually an example of boxing, rather than assignment.
I would also say thatdoes not create the object by assignment. The String exists when the JVM loads the literal; the String literal in fact is the object, which is later assigned to the variable s.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3003
    
    9
Campbell Ritchie wrote:I would say that...is actually an example of boxing, rather than assignment.
I would also say thatdoes not create the object by assignment. The String exists when the JVM loads the literal; the String literal in fact is the object, which is later assigned to the variable s.

Of course, essentially the same argument applies to the "Integer i = 5;" example as well.

The results may be different for "Integer i = 128;" - or it may be the same, created and cached when the Integer class is first loaded.

One could also argue that this ultimately just calls the new Integer() constructor, so it's just a more elaborate example of that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Number of ways of creating object?
 
Similar Threads
dynically craeting the object
how can a class be created, which cannot be instantiated, other than abstract class?
object
Give me some examples
Using System properties or new Properties object?