Wednesday, August 13, 2008

How many ways of creating objects in Java?

How many ways of creating objects in Java?

Well... there aren't many different ways I suppose. But from an application programmer perspective here are the different possible ways of creating objects in Java:-

  • Using new operator - the most commonly used way by far. 'new' is an operator and the only operand it requires is a constructor which will be used for initializting the newly created instance. 'new' operator allocates the memory space and initializes the fields with their default values. Then it executes the code inside the specified constrcutor which normally re-writes the default values of some (or all) fields with the particular values mentioned in the constructor definition.
  • Using clone() method - Cloning (Shallow or Deep) makes a new copy of the specified object. It doesn't call the 'new' operator internally. Object.clone() is a native method which translates into instructions for allocating the memory and copying the data. Remember that even if we override the clone() method either to implement Deep Cloning or to make the clone() method as 'public' but then also we keep the line super.clone() inside the overriden definition which ultimately calls Object.clone() method only. You might wonder about what the 'new' operator actually translates into and how this approach is different from that. Okay... we all know that 'new' does three tasks - allocating the memory, initializing the fields with default values, and calling the specified constructor. Now the first task is same in both the approaches and there would probably be the same native allocator being used in both the cases. But in case of Cloning, the allocated memory is not initialized with default values and also no constructor is called in this case. Only a datacopy instruction will be executed which copies the data from the original object to the cloned object. Read more about Cloing in this article - Cloning in Java >>
  • Using De-Serialization - this can be thought of another different way of creating an object in Java. Again you might wonder that this internally uses 'new' operator only so why to consider this as a new way. Yeah... you're right. It does uses the 'new' operator internally and always calls the default constructor. But two noticeable differences between the two approaches are:- In case of an explict 'new' call we may specify any constructor which is not the case here. It'll always invoke the default constructor. Another difference is that in this case the newly created object is initialized (or better to say re-written as the implicit 'new' call with default constructor would have already initialized the object first with the default value and then with the specified value in the default constructor) with the data from the Input Stream fetched usually from a persistent medium. This step is obviously not involved in an explicit 'new' call.
  • Using Class.forName() and newInstance() - A calss can be dynamically loaded using the Class.formName() method. This method has two variants - one which accepts only a String type parameter which is the qualifies name of the class to be loaded and the other variant expects three parameters - the String type qualifies class name, boolean type flag to specify if the class should be initialized, and the ClassLoader name which should be used to load the class. The former variant also internally calls the three-parameter variant only by assuming the boolean flag as 'true' and the ClassLoader as returned by the getClassLoader() method. That means 'Class.forName("qualified.ClassName")' is internally translated into 'Class.forName("qualifies.ClassName", true, this.getClass().getClassLoader())'. Once the class has been loaded then a call of the method newInstance() on the loaded Class object will first check if the Class object is initialized or not and if not then it will initialize the Class object and create a new object as if the 'new' operator has been called with the default constructor of the class under consideration. Again you may argue how is this different from an explicit 'new' call and it won't take much of an effort to identify the most obvious difference between the two as the inability of this approach to call any other constructor except the default constructor. Another difference is that the newInstance() call may throw a SecurityException as well because it checks if a Security Manager is installed or not and if it finds a security manager then it checks for access to the class as wellas to the package (if any package specified in the qualified name). Either of two checks may throw a SecurityException. This step is obviously not involved with an explicit 'new' call. Another slightly different way of object creation is by using the loadClass() method of the ClassLoader class which returns a Class object of the specified class and then on the returned Class object we may call newInstance() method to create a new object. I've not put this as a completely separate way because I think Class.forName() method also internally calls this method only - either for the explcitly supplied ClassLoader or for the implcitily obtained ClassLoader as discussed above. What's the difference between the two approaches then? The only difference which I can see is that the former checks for the access to the class (and package) and hence it may throw SecurityException if a Security Manager is installed. But the latter doesn't do these checks and hence doesn't throw SecurityException. It's normally advised not to call the loadClass() method directly as almost always you can call Class.forName() by supplying the particular ClassLoader reference.

Is there any other different way of creating objects in Java? I don't think that there is any other way. Using JNI may pop up as another possible way, but I don't think that JNI offers any different way of object creation due to the fact that JNI is not meant for object creation instead it's used for interacting with native methods written in some other languages (mainly in C/C++) and hence a Java class using JNI code is same as any other Java from object creation perspective. Therefore, I think it uses any one of the above mentioned approaches only - most commnonly the explicit 'new' calls. I personally don't have much exposure to JNI and I sincerely welcome comments from the JNI experts if they think otherwise.

Liked the article? You may like to Subscribe to this blog for regular updates. You may also like to follow the blog to manage the bookmark easily and to tell the world that you enjoy GeekExplains. You can find the 'Followers' widget in the rightmost sidebar.



Anil Nivargi said...

Thanks good explaination...finally i got good concept..If anyone want interview questions with answers then please go through this blog

Deepak Kumar Panda said...

1)using new operator
2)using clone method
3)using Class.forName
4)using deserialization
5)using newInstance method
6)using factory method

By Deepak and Samir