Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
senthil sen
Ranch Hand
Posts: 184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how many type or how many kind of way we can create an object in java??
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I count at least four ways...
Why do you care?
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand this question. Please try to ask it differently.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3) de-serialize
4) clone() ??
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class.newInstance()
Constructor.newInstance()
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is also:
String[] str = { "1", "2" };
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic