Reflection Flashcards

(66 cards)

1
Q

What is reflection in Java?

A

Reflection is the ability of a program to inspect and manipulate classes, methods, fields, and constructors at runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What package provides reflection functionality?

A

java.lang.reflect.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What can reflection be used for?

A

Inspecting class metadata, invoking methods, accessing fields, and creating objects dynamically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

When does reflection operate?

A

At runtime.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why is reflection powerful?

A

It allows dynamic behavior without knowing class details at compile time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Example use of reflection?

A

Loading a class by name using Class.forName(“MyClass”).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Can reflection access private members?

A

Yes. Using setAccessible(true), though it may break encapsulation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does reflection help frameworks?

A

It allows frameworks like Spring or Hibernate to inspect classes dynamically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Can reflection create objects?

A

Yes. Using Constructor.newInstance().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Can reflection call methods dynamically?

A

Yes. Using Method.invoke().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Does reflection affect performance?

A

Yes. It is slower than direct method calls.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why is reflection slower?

A

Because operations are resolved dynamically rather than at compile time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Is reflection safe to use everywhere?

A

No. It should be used carefully due to performance and security concerns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What security risk does reflection pose?

A

It can bypass access control and modify private data.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

When should reflection be used?

A

When dynamic behavior is required and cannot be achieved through normal code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Key rule to remember?

A

Reflection = runtime inspection and dynamic invocation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are the main uses of reflection in Java?

A

Reflection is used for runtime inspection, dynamic object creation, method invocation, and framework automation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Why is reflection useful in testing?

A

It allows test frameworks to access private methods and fields for validation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How do IDEs use reflection?

A

IDEs use it to analyze classes, detect methods, and provide features like autocomplete and code inspection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Why is reflection helpful for debugging?

A

It allows runtime inspection of object state and structure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

How do frameworks use reflection?

A

Frameworks like Spring and Hibernate use reflection to scan classes, inject dependencies, and map objects dynamically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Why is reflection important for dynamic programs?

A

It enables programs to load and interact with classes not known at compile time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Which Java feature supports runtime class loading?

A

Class.forName().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

How is reflection used in plugin systems?

A

It loads and executes external modules dynamically.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Why is reflection used in serialization libraries?
To inspect object fields and convert them automatically.
26
How does reflection help dependency injection?
It allows frameworks to discover constructors and inject required objects.
27
Can reflection be used for annotations processing?
Yes. Reflection can read annotations at runtime.
28
Is reflection used in XML or JSON parsing frameworks?
Yes. Parsers use reflection to map data to object fields.
29
Does reflection enable dynamic method invocation?
Yes. Methods can be invoked using Method.invoke().
30
Why should reflection be used cautiously?
It reduces performance and may break encapsulation.
31
When is reflection most appropriate?
When flexibility and runtime adaptability are required.
32
Key rule to remember?
Reflection enables dynamic runtime inspection and execution.
33
How can you access a private method from outside a class in Java?
By using reflection to obtain the Method object, make it accessible, and invoke it.
34
Which API is used to access private methods via reflection?
java.lang.reflect.Method.
35
How do you obtain a private method using reflection?
Call Class.getDeclaredMethod("methodName", parameterTypes).
36
Why use getDeclaredMethod instead of getMethod?
getDeclaredMethod retrieves private methods, while getMethod retrieves only public ones.
37
What does setAccessible(true) do?
It bypasses Java access checks to allow access to private members.
38
How do you invoke a private method using reflection?
Call method.invoke(objectInstance, arguments).
39
Example flow for calling private method?
Load class → create instance → get method → set accessible → invoke.
40
What exception may occur when using reflection incorrectly?
NoSuchMethodException, IllegalAccessException, InvocationTargetException.
41
Can reflection access private fields as well?
Yes. Using Field and setAccessible(true).
42
Is accessing private methods via reflection recommended?
Generally no. It breaks encapsulation and should be used only when necessary.
43
Why do frameworks use reflection to access private members?
To inject dependencies or configure objects dynamically.
44
Does reflection ignore compile-time access rules?
Yes. Access checks can be bypassed at runtime.
45
What security risk exists when accessing private methods?
It can expose internal logic and sensitive data.
46
Can reflection call static private methods?
Yes. Pass null as object instance when invoking.
47
Does reflection affect performance?
Yes. Reflection calls are slower than direct method calls.
48
Key rule to remember?
Reflection can access private methods by overriding access checks.
49
How can we access private method of a class from outside the class?
We can use Reflection to access private method of a class from outside the class. IN Java, we use getDeclaredMethod() to get instance of a private method. Then we mark this method accessible and finally invoke it. In following sample code, we are accessing private method message() of class Foo by Reflection. FileName: Foo.java public class Foo { private void message(){System.out.println("hello java"); } } FileName: FooMethodCall.java import java.lang.reflect.Method; public class FooMethodCall{ public static void main(String[] args)throws Exception{ Class c = Class.forName("Foo"); Object o= c.newInstance(); Method m =c.getDeclaredMethod("message", null); m.setAccessible(true); m.invoke(o, null); } }
50
How can you create an object dynamically at runtime in Java?
By using reflection to load a class and instantiate it using Constructor.newInstance().
51
What class is used to dynamically load classes?
Class.
52
Which method loads a class by name?
Class.forName("ClassName").
53
How do you create an instance using reflection?
Class c = Class.forName("MyClass"); Object obj = c.getDeclaredConstructor().newInstance();
54
What is the modern recommended method for instantiation?
getDeclaredConstructor().newInstance().
55
Why is Class.newInstance() discouraged?
It is deprecated because it suppresses checked exceptions and has limited flexibility.
56
What does Constructor.newInstance() allow?
It lets you call constructors with parameters.
57
Example of constructor reflection?
Constructor cons = c.getConstructor(String.class); Object obj = cons.newInstance("value");
58
Can reflection instantiate private constructors?
Yes. By calling setAccessible(true).
59
Why create objects dynamically?
When class name is not known until runtime.
60
Where is dynamic instantiation commonly used?
Frameworks, plugin systems, dependency injection containers.
61
What exception occurs if class name is wrong?
ClassNotFoundException.
62
What exception occurs if constructor not found?
NoSuchMethodException.
63
Does dynamic instantiation require compile-time knowledge of class?
No. Only class name string is needed.
64
Does reflection-based instantiation affect performance?
Yes. It is slower than normal object creation.
65
Is dynamic instantiation type-safe?
No. Casting is usually required after creation.
66
Key rule to remember?
Runtime object creation = Class loading + constructor invocation via reflection.