Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

In how many ways we can create an object? could you Explain with example if possible.  RSS feed

 
Madhava Thamatama
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In how many ways we can create an object? could you Explain with example if possible.

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can find many useful links, if you google
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How many ways do you know, and what are they?
 
Madhava Thamatama
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as my knowledge we can create object in 3 ways 1)by using new key word 2)class.foName() 3)clone() method .
could yu please tell me if you know any others ways to create object ?
 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
De-serialization comes to mind.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some more I can think of:

- Class.newInstance()
- Constructor.newInstance(Object...)
- Auto-boxing a primitive
- Use of String literal and array literals
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:Some more I can think of:

- Class.newInstance()
- Constructor.newInstance(Object...)
- Auto-boxing a primitive
- Use of String literal and array literals


Auto-boxing will not necessarily create an object. It can in some cases just return an instance from a cache.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:Auto-boxing will not necessarily create an object. It can in some cases just return an instance from a cache.

Same with the String literals. But it can produce a new object.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True but autoboxing is nothing more than syntactic sugar. It's just a call to <Wrapper>.valueOf() and that may call a constructor. So it is debatable if auto-boxing creates it or if it's just a couple of linked calls to a constructor.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, both autoboxing and deserialization just end up calling constructors - one via the new keyword, and one via reflection. Not sure they really count as separate ways to create something. Then again, I'm not sure this distinction really matters - it depends why the person asking the question needs the information. Seems like a silly trivia question. Arguing the semantics doesn't seem that interesting.
 
Rob Spoor
Sheriff
Posts: 21090
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:Yeah, both autoboxing and deserialization just end up calling constructors - one via the new keyword, and one via reflection.

De-serialization does not call any constructor. You can prove that easily:
Output:
Test constructor called
false

So the constructor is only called once, on line 12, and the two instances are not the same. That means that de-serialization created a new object without calling the constructor.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm, interesting - the situation for constructors in serialization is more complex than I realized. I overlooked that only the constructors of non-serializable superclasses are called during deserialization. So yes, there are cases where constructors aren't called in deserialization, even as there are cases where they are. But you're right, the former cases are sufficient for inclusion in this list.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question is a frequently asked question on the forums here. Every now and then somebody comes along that asks this exact same question. (Is this a standard question in interviews or at some school?).

If you do a search in the forums, you'll find older discussions and answers for this question.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!