Wednesday, August 27, 2008

Diff java.lang.Class instances for the same Type ?


One of our visitors (Priyank) asked these questions via email. Thanks Priyank for your participation.

Can we have two java.lang.Class instances of the same Type?


Yeah... we can have any number of java.lang.Class instances of the same type provided we get the Type loaded by a different ClassLoader instance everytime. Please note that even if we use the same ClassLoader class, but if we use different instances of that class to load a Java Type then each time we'll get a different java.lang.Class instance for the loaded Type. If no ClassLoader instance is explicitly specified then the default ClassLoader instance (Application Class Loader for user classes - Read more in this article - Class Loaders in Java >>) is used which the user doesn't need to create instead JVM creates and maintains for the users.

The loadClass method of the ClassLoader class is used to load any Java Type. This method has two variants:

  • public Class loadClass(String name) - this variant accepts only a String type parameter asking the caller to supply the name of the class
  • protected Class loadClass(String name, boolean resolve) - this variant accepts one additional boolean type parameter asking the caller to specify if the loaded class should be linked immediately or not. Default value of this parameter is false and hence a loadClass(className1) call is internally converted into loadClass(className1, false) call
As you would have noticed that both these methods are instance methods and hence they can be called only on an instance of type ClassLoader. You can create your own instances of any valid ClassLoader type and call the loadClass method to load a Java class to have separate java.lang.Class instances every time you use a separate instance of type ClassLoader to call the loadClass method upon.

To understand how a Type is loaded, linked and initialized in Java you may like to read a separate article discussing this in detail - Loading, Linking, & Initialization of Types in Java >>

How can we change the properties of a singleton object?

Well... it's no different than changing the properties of any other object. The convention is to have public setter methods for all the mutable fields. Singleton doeesn't really play any role here. After all that's also an object (though we have only one shared copy of it) and it can be changed by calling the corresponding setter methods on that instance. I hope this answers your question otherwise please let me know if you meant something else.

Does volatile maintain mutual exclusion as well?

No... volatile doesn't maintain mutual exclusion. Declaring a variable volatile simply solves the communication problem i.e., every thread will read the last modified value of the variable. To implement this the optimizer simply skips creating local cache variables (local to the thread) of any volatile variable and all threads need to read the value of such a variable (of course the variable should be shared) from one copy only.

Synchronization helps solving two major issues in a multithreading environment - mutual exclusion and communication problem. volatile is a simpler and faster way of overcoming just the latter one. In case your multithreading requirements doesn't need mutual exclusion (for example: if you're sure that at one point of time only one of the several threads will try to modify the value of a shared variable) then using a synchronized block will be a waste as such a simple case can be more efficiently solved by using volatile only. As already explained that it's internally implemented by enforcing the compiler/optimizer to have only one copy of the variable and donesn't really need any locking/unlocking mechanism (as required by synchronization) hence it'll always be significantly faster than synchronization. More about synchronization in this article - Synchronization in Java >>

Note: Hey Priyank, feel free to correct me in case I misinterpreted any of your questions or if you're not satisfied with the answers.

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.



Share/Save/Bookmark


2 comments:

Anonymous said...

Hey Geek, thanks for all the answers. I'm completely satisfied with thm. Regarding the question 'how to chg properties of a singleton' - I was asked this que in my last tech interview and I think they just wanted to confuse with singleton term.

Your article on Class Loaders in Java is also very well written and easy to understand. GeekExplains rocks!

Thanks Again,
Priyank Varma

Anil said...

I was also looking for the answers to these questions.
Thanx