This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes how many type or how many kind of way we can create an object in java?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how many type or how many kind of way we can create an object in java??" Watch "how many type or how many kind of way we can create an object in java??" New topic
Author

how many type or how many kind of way we can create an object in java??

senthil sen
Ranch Hand

Joined: Oct 10, 2002
Posts: 184
how many type or how many kind of way we can create an object in java??
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
I count at least four ways...
Why do you care?


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
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
I don't understand this question. Please try to ask it differently.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
In general you make a call to any constructor of the class using the "new" keyword.
The exception to this is the String class, which allows you to create an object using a shortcut but creating a String literal directly.
And senthil,
Please change your name to be compliant with JavaRanch's naming policy.
Your displayed name should be 2 separate names with more than 1 letter each. We really would prefer that you use your REAL name.
You can change your name: here.
Thanks,
Cindy


"JavaRanch, where the deer and the Certified play" - David O'Meara
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Cindy Glass:
In general you make a call to any constructor of the class using the "new" keyword.
The exception to this is the String class, which allows you to create an object using a shortcut but creating a String literal directly.

Yes, that are two of the four ways I was referring to...
Vin Kris
Ranch Hand

Joined: Jun 17, 2002
Posts: 154
3) de-serialize
4) clone() ??
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Class.newInstance()
Constructor.newInstance()
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Yeah - but in any of those methods, when you look under the covers, eventually somewhere there is a line of code that makes a "new" Object using the new keyword.
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
But if these reflection methods used "new Object()", they'd create a plain old Object instead of an instance of the desired class. So something unusual is going on too.
[ October 28, 2002: Message edited by: Ron Newman ]
Vin Kris
Ranch Hand

Joined: Jun 17, 2002
Posts: 154
3) de-serializing readObject()

Reading an object is analogous to running the constructors of a new
object.
Memory is allocated for the object and initialized to zero (NULL).
No-arg constructors are invoked for the non-serializable classes and then
the fields of the serializable classes are restored from the stream starting
with the serializable class closest to java.lang.object and finishing with
the object's most specific class.

4) clone() - native method.
I think it just gets required block of memory and copies the original object into this block of memory byte-by-byte -> Shallow Copy. Correct me.
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

There is also:
String[] str = { "1", "2" };
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Yeah - but in any of those methods, when you look under the covers, eventually somewhere there is a line of code that makes a "new" Object using the new keyword.
Not for reflection, I think - Ron's right. You'll have a hard time finding a Java "new" operator buried in there.
Reading an object is analogous to running the constructors of a new object.
"Analogous" is potentially a bit too vague, but on inspecting the source code, it seems that what happens is that reflection is used to call a no-arg constructor. (Ultimately this is done in ObjectInputStream.readOrdinaryObject() for most cases.) So we may regard deserialization as a subset of reflection, I suppose.
4) clone() - native method.
I think it just gets required block of memory and copies the original object into this block of memory byte-by-byte -> Shallow Copy. Correct me.

Basically, yes. This is apparently implemented in community source for 1.4.0 at hotspot/src/share/vm/prims/jvm.cpp. (The community source edition contains native code as well.) It also does stuff like checking that the object is Cloneable and registering any finalizers - but the main cloning is achieved with a simple copying of bytes. So clone() does indeed create a new object instance without using the new operator or reflection - at least in the community source edition.
Another way to create an object, similar to String literals, is with a class literal (like Object.class, String.class, etc). Often these just refer to classes that have already been loaded, and thus don't really "create" an object. And even when you do try to refer to a class not already loaded, you end up forcing the compiler and JVM to load the class before it gets around to executing the line that contains the class literal. So one could argue that the literal doesn't "create" an instance so much as it ensures that the instance has already been created. Which is just a little bit weird to wrap your head around, I admit. But these same arguments are true for String literals as well - so we include one String literals as a way to create an object, we should also include class literals.
[ October 28, 2002: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
In 1.3.1, Class.newInstance() just calls Class.newInstance0() which is a private native method.
Constructor.newInstance() is a public native method.
Vin Kris
Ranch Hand

Joined: Jun 17, 2002
Posts: 154
This is what the documentation says about serialization.

Serialization does not read or assign values to the fields of any object that does not implement the java.io.Serializable interface. Subclasses of Objects that are not serializable can be serializable. In this case the non-serializable class must have a no-arg constructor to allow its fields to be initialized. In this case it is the responsibility of the subclass to save and restore the state of the non-serializable class.
Reading an object is analogous to running the constructors of a new object. Memory is allocated for the object and initialized to zero (NULL). No-arg constructors are invoked for the non-serializable classes and then the fields of the serializable classes are restored from the stream ...

Therefore, no-arg constructors are invoked only for initializing a non-serializable super class's variables during the process of de-serialization.
I didn't have enough time to look at the source code for readObject() but looks like certain constants from java.io.ObjectStreamConstants are used to identify the next object in the stream. I have to confirm this - but does this logic classify under reflection?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I thought it was clear the first time you quoted the spec - yes, the no-arg constructor is used for deserialization. What the spec does not tell us, and which I was addressing, is whether the constructor is invoked via "new", through reflection, or through some other mechanism. And while it's true that many of the steps involved in reading and interpreting the serialized object have nothing to do with reflection, the critical step of actually creating an instance does depend on reflection. That's the part that seems relevant to the original question, IMO.
[ October 28, 2002: Message edited by: Jim Yingst ]
Vin Kris
Ranch Hand

Joined: Jun 17, 2002
Posts: 154
No Hard feelings Jim - I guess I didn't come out properly. I just wanted to confirm that the logic used with constants was Reflection (I read the source code only after your explanation). Now I know its not. Thanks.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Sorry about that - I was a bit testy due to a headache. Fortunately the ibuprofen seems to be kicking in now...
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Even with reflection you DO create a "new Constructor" object using the new keyword and then invokes the constructor of the passed class which gets returned as an Object.
So I guess that it depends on how literal you want to take the question.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how many type or how many kind of way we can create an object in java??
 
Similar Threads
creating an object
Ways to create an object?
creating object
Objects
creating object out of class of same name