-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16. java reflection.txt
More file actions
53 lines (26 loc) · 1.97 KB
/
16. java reflection.txt
File metadata and controls
53 lines (26 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
reflection:
-feature of java
-helps to inspect and manipulate the runtime behaviour of the applications running in the JVM
-because it helps us to get the details about the multiple entities of a java program even if we dont know their name or any
other identifier
-it enables the examination and modification of classes, interfaces, fields, and methods at runtime, even if their
names are not known until runtime
-basically, har chij jan saktein hain during runtime
classes present in the reflection API:
-Class<T>: provides us the class and interface object for the classes and interfaces and then we can extract every information about that class or interface
-Field: provides the object for the field and then we can extract every information about the field of the class or interface
-Method: provides the object for the method and then we can extract every information about the method of the class or interface
-Constructor<T>: provides the object for the constructors and then we can extract every information about the constructor of the class
-Modifier: provides us with static methods and constants, to decode classes and member class modifiers
we can invoke the methods during runtime, regardless of the access specifiers, using the reflections
we can also create new instances of the objects by invoking the constructor, even if they are private
we can read and write to feilds, including private fields also, by overriding the access checks
in short, reflection provides us the ability to tackle the accessibility
considerations and best practices: (bmp)
ways of getting the class/interface object:
Class<?> clazz = Myclass.class (way 1)
Myclass obj = new Myclass()
Class<?> clazz = obj.getClass() (way 2)
Class<?> clazz = Class.forName("") (way 3)
once we get the class object, we can do multiple things with it, using other methods available in the reflect package
and therefore, we can also do the same for all the other classes like modifiers, fields and all