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

Interview question

Silvio Esser
Ranch Hand

Joined: Nov 05, 2005
Posts: 58
What are the three ways to create an instance of a class in Java?
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

You can create objects in Java using the following methods: 1. using the new operator. 2. using the clone() method, 3. deserializing 4. calling newInstance() method of the Class class.

I think that's all of the ways to create an object in Java.

You probably, almost always, want to use the new operator to create an object.

There is a book on amazon.com about 220 potential Java interview questions:

ISBN-10: 1411668243
ISBN-13: 978-1411668249

Kaydell
[ June 08, 2007: Message edited by: Kaydell Leavitt ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Two other ways are using a String literal

String myString = "myString",

and using autoboxing

Integer integerObject = 42;


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Using newInstance() is a special case of the more general technique: uusing reflection to invoke a constructor. The newInstance() method gets a non-arg constructor (if it exists) and invokes it. But you can also use the getConstructor() method to get a particular constructor with a particular set of argument types. E.g.

Using a string literal and using autoboxing both can create new objects. But they don't always - they may simply reference objects that were already there. Ultimately, autoboxing relies on using "new", as seen in (for example) the source code to Integer.valueOf() and Integer.IntegerCache.

It would also be possible to create an object by using JNI to call native code which creates a Java object, or by using bytecode engineering or non-Java JVM languages to load instructions into the JVM to create an object. Of course one may argue that this is no longer "in Java", which was part of the original question.

Of the Java techniques listed, cloning and deserializing are the only two which do not ultimately invoke any constructor. In that sense, we may say that there are just three ways to create an object in Java:

1. invoking a constructor
2. cloning
3. deserializing


"I'm not back." - Bill Harding, Twister
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You probably, almost always, want to use the new operator to create an object.


That could lead to some debate. In advanced circles folks focus on loose coupling, and knowing what class to use new on is pretty tight coupling. With configurable factories or a "dependency injection" framework like Spring you might almost never use new.

If that tickles your interest - or makes you doubt my sanity - scroll on down to the OO, UML, etc forum and post some questions to dig deeper.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Silvio Esser
Ranch Hand

Joined: Nov 05, 2005
Posts: 58
Originally posted by Jim Yingst:


Of the Java techniques listed, cloning and deserializing are the only two which do not ultimately invoke any constructor. In that sense, we may say that there are just three ways to create an object in Java:


The default implementation of Object.clone() in Java uses the "new" operator
to create an new instance and shallow-copy the contents from the old one.
So the constructor is called.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Silvio Esser:


The default implementation of Object.clone() in Java uses the "new" operator
to create an new instance and shallow-copy the contents from the old one.
So the constructor is called.


Sorry, but no. Object.clone() is implemented in native code. No "new", no constructor.


[Jess in Action][AskingGoodQuestions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671

The output shows that the second Test is created without ever calling the constructor:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interview question