java lang Class Class - java.lang.Class Class in Java
java.lang.Class Class
Class Class in Java
Class represents classes and interfaces in a running Java application. An enum is a kind of class and an annotation is a kind of interface. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects.
Class has no public constructor. Instead Class objects are constructed automatically by the Java Virtual Machine as classes are loaded and by calls to the defineClass method in the class loader.
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 . This method is inherited from java.lang.Object class .
What is the use of class java.lang.Class in Java ?
java.lang.Class is one of the most important class in Java . It is very useful , it provides several utility methods getClass(), forName() which is used to find and load a class , you might have used to load Oracle , MySQL and MSSQL drivers class . It also provides methods like Class.newInstance() which is the backbone of reflection and allow you to create an instance of a class without using new() operator . This class has no public constructor and its instance is created by JVM when a class is loaded . Another use of java.lang.Class is while implementing equals() method to check whether two objects are of the same type or not . It is a final class , so we cannot extend it .
We need java.lang.Class.forName() and java.lang.Class.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 Java application . This is the reflective way of creating an object which is one of the most powerful feature of Java and which makes way for many frameworks e.g. Spring , Struts which uses Java reflection .
What does Class <?> Mean in Java ?
Class is a parameterizable class, hence you can use the syntax Class<T> where T is a type. By writing Class<?> , you're declaring a Class object which can be of any type ( ? is a wildcard). The Class type is a type that contains meta-information about a class .
Class Class<T>
java.lang.Object java.lang.Class<T>Instances of the class Class represent classes and interfaces in a running Java application. An enum is a kind of class and an annotation is a kind of interface.
How to create an object without using new operator in Java ?
By using the method java.lang.Class.newInstance() , which is the backbone of reflection and allow you to create an instance of a class without using new() operator . First you have to create Class object with the help of java.lang.Class.forName() method . The java.lang.Class.forName() actually loads the Class in Java but doesn't create any Object. To Create an Object of the Class, you have to use the newInstance method of the Class class.
Can you create an object without using new operator in Java ?
Yes, We can create an object without using new operator. There are some other ways to create objects other than using new operator. But almost all object creation in java is done through new operator only.
Example to create an object without using new operator
Class c = Class.forName("java.lang.String");
String object = (String) c.newInstance();
How many ways to create an object without using new operator in Java ?
We can create an object without using new operator. There are some other ways to create objects other than using new operator.
-
Using newInstance() Method or Dynamic class Loading (Reflection)
Have you ever tried to connect to a Database using the JDBC driver in Java ? you must have seen java.lang.Class.forName(). We can also use it to create the Object of a Class. Class.forName actually loads the Class in Java but doesn't create any Object. To Create an Object of the Class, you have to use the newInstance Method of the Class class.
Class c = Class.forName("java.lang.String");
String object = (String) c.newInstance(); -
Using clone() method
We can also use Clone() Method to create a copy of an existing Object . A class needs to implement Cloneable Interface otherwise it will throw CloneNotSupportedException .
java.lang.String my_string = new String("java.lang.Class Example");
String clone_object = my_string.clone(); -
Using Object Deserialization
Object deserialization can also be used to create an Object . It produces the opposite of serializing an Object .
ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyClass object = (MyClass) inStream.readObject(); -
Creating string and array objects
String s = "string object";
int [ ] a = {1, 2, 3, 4};
In this tutorial you can learn how to use Class class.
java.lang.Class Class Example
/* This class is supporting class for Examples of Class class Save with file name Student.java */ public class Student { // FIELDS OF THE Student CLASS public int sid; public String sname; public String scollege; // CONSTRUCTORS OF THE Student CLASS public Student(){} public Student(int sid, String sname, String scollege) { this.sid = sid; this.sname = sname; this.scollege = scollege; } // SETTER METHODS OF THE Student CLASS public void setStudentID(int sid) { this.sid = sid; } public void setStudentName(String sname) { this.sname = sname; } public void setStudentCollege(String scollege) { this.scollege = scollege; } // GETTER METHODS OF THE Student CLASS public int getStudentID() { return this.sid; } public String getStudentName() { return this.sname; } public String getStudentCollege(String scollege) { return this.scollege; } public String toString() { return (this.sid+" "+this.sname+" "+this.scollege); } }
How to get class name, package and canonical name of a class using Java
/* How to get class name, package and canonical name of a class using Java Example Save with file name ClassExample1.java */ import java.lang.reflect.*; public class ClassExample1 { public static void main(String args[]) { // Student CLASS DECLARATION Student s; // CREATE INSTANCE FOR Student CLASS s = new Student(1, "Java Class Example", "https://www.hudatutorials.com"); // Class CLASS DECLARATION Class c; // CREATE INSTANCE FOR Class CLASS // getClass() METHOD IS Object CLASS METHOD // IT IS CALLED FROM Student CLASS // BECAUSE Object CLASS IS SUPER (PARENT) CLASS c = s.getClass(); // RETURNS THE CLASS NAME System.out.println("Class Name : " + c.getName()); // RETURNS THE CLASS SIMPLE NAME System.out.println("Class Simple Name : " + c.getSimpleName()); // RETURNS THE CLASS CANONICAL NAME System.out.println("Class Canonical Name : " + c.getCanonicalName()); // RETURNS THE CLASS PACKAGE // RETURNS null IF NO PACKAGE DECLARED System.out.println("Package of the Class : " + c.getPackage()); } }
How to get class Fields, Methods, Constructors using Java
/* How to get class Fields, Methods, Constructors using Java Example Save with file name ClassExample2.java */ import java.lang.reflect.*; public class ClassExample2 { public static void main(String args[]) { // Student CLASS DECLARATION Student s; // CREATE INSTANCE FOR Student CLASS s = new Student(1, "Java Class Example", "https://www.hudatutorials.com"); // Class CLASS DECLARATION Class c; // CREATE INSTANCE FOR Class CLASS // getClass() METHOD IS Object CLASS METHOD // IT IS CALLED FROM Student CLASS // BECAUSE Object CLASS IS SUPER (PARENT) CLASS c = s.getClass(); // GETTING THE FIELDS OF CLASS Field fields[] = c.getFields(); System.out.println("FIELDS"); for(int i=0;i<fields.length;i++) { System.out.println("Field "+ (i+1) + " => " + fields[i]); } System.out.println(); // GETTING THE CONSTRUCTORS OF CLASS Constructor con[] = c.getConstructors(); System.out.println("CONSTRUCTORS"); for(int i=0;i<con.length;i++) { System.out.println("Constructor "+ (i+1) + " => " + con[i]); } System.out.println(); // GETTING THE METHODS OF CLASS Method m[] = c.getMethods(); System.out.println("METHODS"); for(int i=0;i<m.length;i++) { System.out.println(); System.out.format("Method %3d => " + m[i], (i+1)); } } }