• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In how many ways we can create an object? could you Explain with example if possible.

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you can find many useful links, if you google
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
De-serialization comes to mind.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4830
74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Make yourself as serene as a flower, as a tree. And on wednesdays, as serene as this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic