java.lang.Class is one of the most important classes in java but mostly overlooked by Java developers. It is very useful in the sense that it provides several utility methods like getClass(), forName() which is used to find and load a class, you might have used to load Oracle or MySQL drivers. It also provides methods like Class.newInstance() which is the backbone of reflection and allows you to create an instance of a class without using a new() operator. The class has no public constructor and its instance is created by JVM when a class is loaded.
The object of class Class is also used to represent classes, enum, interfaces, and annotation in a running Java application. The primitive types like byte, short, char, int, float, double and boolean are also represented by Class instances.
You can get the corresponding Class instance by using class literals like int.class, float.class or boolean.class. It is also used to represent an instance of the array in Java. Every array with the same type and same dimension shares the same instance of class Class.
Another use of java.lang.Class is while implementing equals() method to check whether two objects are of the same type or not.
And, If you are new to the Java world then I also recommend you go through The Complete Java MasterClass on Udemy to learn Java in a better and more structured way. This is one of the best and up-to-date courses to learn Java online.
Suppose you create two instances of a class called Person like
In this case, it will print "A and B are instances of the same class" because they are both instances of class Person.
We need forName() and newInstance() because many times it happens that we don’t know the name of the class to instantiate while writing code, we may get it from config files, database, network or from any upstream Java or C++ application.
This is what we called the reflective way of creating an object which is one of the most powerful features of Java and which makes way for many frameworks like Spring, Struts which use Java reflection.
Further Learning
The object of class Class is also used to represent classes, enum, interfaces, and annotation in a running Java application. The primitive types like byte, short, char, int, float, double and boolean are also represented by Class instances.
You can get the corresponding Class instance by using class literals like int.class, float.class or boolean.class. It is also used to represent an instance of the array in Java. Every array with the same type and same dimension shares the same instance of class Class.
Another use of java.lang.Class is while implementing equals() method to check whether two objects are of the same type or not.
And, If you are new to the Java world then I also recommend you go through The Complete Java MasterClass on Udemy to learn Java in a better and more structured way. This is one of the best and up-to-date courses to learn Java online.
java.lang.Class class in Java
Every time JVM creates an object, it also creates a java.lang.Class object that describes the type of the object. All instances of the same class share the same Class object and you can obtain the Class object by calling the getClass() method of the object. By the way, this method is inherited from java.lang.Object class.Suppose you create two instances of a class called Person like
Person A = new Person(); Person B = new Person(); if(A.getClass() == B.getClass()){ System.out.println("A and B are instances of same class"); }else{ System.out.println("A and B are instances of different class"); }
In this case, it will print "A and B are instances of the same class" because they are both instances of class Person.
We need forName() and newInstance() because many times it happens that we don’t know the name of the class to instantiate while writing code, we may get it from config files, database, network or from any upstream Java or C++ application.
This is what we called the reflective way of creating an object which is one of the most powerful features of Java and which makes way for many frameworks like Spring, Struts which use Java reflection.
Further Learning
Nice article... Thanks for sharing the information. Good to refresh known stuffs.
ReplyDeleteUsed by Classloaders too when loading classes.
ReplyDelete@Jegan and Anonymous , good to know that you find this java class blog post useful, thanks for your comments.
ReplyDeleteThanks for your Comment Michee. good to know that you like my Java Blog. your suggestions will always be welcome.
ReplyDeletehi javin, is class.forName() and class.newInstance() are factory design patterns right?
ReplyDeletemr javin i m big reader of ur blog, as usual it is good article.
ReplyDeletei hv one doubt can v use getclass() instead of instance of operator to check 2 objs r blong to same class?
Javin, does it mean that only one instance of java.lang.Class is made and it shared among all instances of the class. Is it singleton in nature?
ReplyDeleteNice articles you have :)
ReplyDelete@Javin , Can you pls check my blog ..http://saralsaxena.blogspot.in/2014/11/different-ways-to-create-objects-in-java.html
ReplyDeleteI have added the alternative ways in detail , Pls lem me know any feedback for my blog(http://saralsaxena.blogspot.in)
A very detailed article ...Thanks a lot
ReplyDelete