• Post Reply Bookmark Topic Watch Topic
  • New Topic

Ways for creating an object  RSS feed

 
vijayk kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Members,

Is it true that an object can be created in two ways.
one is by new() and another by deserialization.
What is the exact differnce between these two?
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posetd by vijayk kumar
Dear Members,

Is it true that an object can be created in two ways.
one is by new() and another by deserialization.
What is the exact differnce between these two?


In deserialization, you are not creating an object rather you are getting the whole state of object the entire graph of object which is already there either on your JVM or on a remote JVM.

With new, you are actually creating a new instance of a class.

In String you can also create an instance by using string literals which is different from new. Class is instantiated by new when your program execution begins. Whereas, string literal objects are created at class load time.

Naseem
 
Adam Nace
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're list is somewhat incomplete.

You can use the new operator. You can also use deserialization. Furthermore, you can also use clone().

You can also you reflection, to call someClass.newInstance(args).

When using RMI, you are also creating a new object, out of the stub class for the remote object, which also happens implicitly, without using the new operator. This *technically* occurs in deserialization, but it actually replaces the deserialized object with a completely new object of the stub class, if I'm not mistaken.

Furthermore, in Java 1.5, you can use autoboxing (although technically, this is accomplished with linguistic support, so I suspect that the compiler treats it as if you had called new and one of the appropriate type's constructors (or more likely valueOf, which may ultimately do so)).

Furthermore, in Java 1.5, Enum values are created without calling the new operator, using cloning or deserialization, or autoboxing. Once again, though, they are created using linguistic support, so I suspect that they compiler is treating them as if you had called new pn the appropriate constructor.

This is not necessarily an exhaustive list either, but is more complete (as a note, I don't consider factory methods as creating objects without the new keyword, because the new keyword will probably be used inside the factory method).

- Adam
[ July 31, 2006: Message edited by: Adam Nace ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

(as a note, I don't consider factory methods as creating objects without the new keyword, because the new keyword will probably be used inside the factory method)

Yes you did - most of your additions to the "exhaustive list" are just that
 
Adam Nace
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The above code IS basically explicitly using the new operator, it's just doing it inside the factory method.

The same CAN NOT be said about the clone method, which is native. It also cannot be said about the Class.newInstance method, which uses reflection to get and invoke a constructor. Which brings me to using a Constructor (reflection) which ultimately delegates to ConstructorAccessor, which is a sun package class which for which the source code is not distributed. I imagine, however, that it ultimately would have to be a native call.

I can't comment on how it works in RMI, because I haven't looked.

As for auto-boxing and Enums, they're done using compile time support. The compiler may well be replacing lines of code with ones containing the new keyword, but it is this is done by the compiler, not by the code.

Ultimately, you won't find the keyword new in the source code for any of the methods I outlined above, no matter how far you trace the call heriarchy -- at least, not as far as I am aware. I may be wrong on one or two of them.

- Adam
[ August 01, 2006: Message edited by: Adam Nace ]
 
vijayk kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ThankYou,
I am clear now!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!