Skip to content

Commit bc2b4d8

Browse files
committed
feat: Add ArrayListDemo7 to explore constructors
and methods via reflection This demo showcases how to use Java Reflection API to inspect the internal structure of the ArrayList class. Key highlights: • Obtains the Class object for java.util.ArrayList using ArrayList.class. • Retrieves all declared constructors with getDeclaredConstructors(). • Retrieves all declared methods with getDeclaredMethods(). • Iterates over constructors and methods, printing their names. Why this is useful: • Demonstrates reflection capabilities for runtime class inspection. • Helps understand how many constructors and methods exist inside core library classes like ArrayList. • Useful for debugging, framework design, and meta-programming tasks where behavior adapts based on runtime class analysis. Output: - Prints the list of all constructor signatures of ArrayList. - Prints the names of all methods (public, protected, private, and package-private declared within ArrayList). Signed-off-by: https://github.com/Someshdiwan <someshdiwan369@gmail.com>
1 parent e3ee9b1 commit bc2b4d8

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.lang.reflect.Constructor;
2+
import java.lang.reflect.Method;
3+
import java.util.ArrayList;
4+
5+
public class ArrayListDemo7 {
6+
public static void main(String[] args) {
7+
// Get the Class object of ArrayList
8+
Class<?> classOB = ArrayList.class;
9+
10+
// Get all constructors of ArrayList
11+
Constructor<?> [] declaredConstructors = classOB.getDeclaredConstructors();
12+
13+
// Get all methods of ArrayList
14+
Method[] declaredMethods = classOB.getDeclaredMethods();
15+
16+
// Print all constructor names
17+
for(Constructor<?> constructor : declaredConstructors) {
18+
System.out.println(constructor.getName());
19+
}
20+
21+
// Print all method names
22+
for(Method method : declaredMethods) {
23+
System.out.println(method.getName());
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)