Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Number of ways of creating object?

 
Saral Saxena
Ranch Hand
Posts: 203
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 100
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be you can go through this http://stackoverflow.com/questions/95419/what-are-all-the-different-ways-to-create-an-object-in-java
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4395
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can create an object by assignment.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:you can create an object by assignment.
Can you really?
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4395
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah, that's what i meant.
 
Akhilesh Trivedi
Ranch Hand
Posts: 1599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Deems wrote:

Perhaps he has in mind something like


That way this should work too.
 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3076
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic