What Is The Role Of Shape “Java.Lang.Class” Inwards Java?
java.lang.Class is 1 of the almost of import flat inward coffee exactly generally overlooked past times Java developers. It is really useful inward the feel that it provides several utility methods e.g. getClass(), forName() which is used to notice too charge a class, yous powerfulness convey used to charge Oracle or MySQL drivers. It likewise provides methods similar Class.newInstance() which is the backbone of reflection too let yous to create an example of a flat without using new() operator. The flat has no world constructor too its example is created past times JVM when a flat is loaded. The object of flat Class is likewise used to stand upwards for classes, enum, interfaces too notation inward a running Java application. The primitive types e.g. byte, short, char, int, float, double too boolean are likewise represented past times Class instances.
You tin larn the corresponding Class example past times using flat literals e.g. int.class, float.class or boolean.class. It is likewise used to stand upwards for an example of the array inward Java. Every array alongside the same type too same dimension portion the same example of flat Class.
Another role of java.lang.Class is spell implementing equals() method to cheque whether ii objects are of the same type or not.
Suppose yous create ii instances of flat called Person e.g.
In this case, it volition impress "A too B are instances of the same class" because they are both example of flat Person.
We involve forName() too newInstance() because many times it happens that nosotros don’t know the parent of the flat to instantiate spell writing code , nosotros may larn it from config files, database,network or from whatever upstream Java or C++ application.
This is what nosotros called the reflective agency of creating an object which is 1 of the almost powerful characteristic of Java too which makes agency for many frameworks e.g. Spring ,Struts which uses Java reflection.
Further Learning
Java Fundamentals Part 1 too 2
Java Web Fundamentals
Head First Java 2d Edition
You tin larn the corresponding Class example past times using flat literals e.g. int.class, float.class or boolean.class. It is likewise used to stand upwards for an example of the array inward Java. Every array alongside the same type too same dimension portion the same example of flat Class.
Another role of java.lang.Class is spell implementing equals() method to cheque whether ii objects are of the same type or not.
java.lang.Class flat inward Java
Every fourth dimension JVM creates an object, it likewise creates a java.lang.Class object that describes the type of the object. All instances of the same flat portion the same Class object too yous tin obtain the Class object past times calling the getClass() method of the object. By the way, this method is inherited from java.lang.Object class.Suppose yous create ii instances of flat called Person e.g.
Person H5N1 = new Person(); Person B = new Person(); if(A.getClass() == B.getClass()){ System.out.println("A too B are instances of same class"); }else{ System.out.println("A too B are instances of dissimilar class"); }
In this case, it volition impress "A too B are instances of the same class" because they are both example of flat Person.
We involve forName() too newInstance() because many times it happens that nosotros don’t know the parent of the flat to instantiate spell writing code , nosotros may larn it from config files, database,network or from whatever upstream Java or C++ application.
This is what nosotros called the reflective agency of creating an object which is 1 of the almost powerful characteristic of Java too which makes agency for many frameworks e.g. Spring ,Struts which uses Java reflection.
Further Learning
Java Fundamentals Part 1 too 2
Java Web Fundamentals
Head First Java 2d Edition
0 Response to "What Is The Role Of Shape “Java.Lang.Class” Inwards Java?"
Post a Comment