0% found this document useful (0 votes)
4 views

Dynamic Class Loading

this is novel of dynamic class loading
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Dynamic Class Loading

this is novel of dynamic class loading
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

// Student.

java file

public class Student


{
public String name;
public Student()
{
this.name = "Janlavchariv";
}
public void setName(String n)
{
this.name = n;
}
public String getName()
{
return this.name;
}
public void greetings() {
System.out.println("Hello from "+this.name);
}
}

// Test.java
//import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;

public class Test


{
public static void main(String[] args)
{
try {
Class<?> dynamicClass = Class.forName("Student");
// Instantiate an object of the dynamically loaded class
Object dynamicObject = dynamicClass.newInstance();
// Invoke a method on the dynamically loaded object
dynamicClass.getMethod("greetings").invoke(dynamicObject);
System.out.println("My name is " +
dynamicClass.getMethod("getName").invoke(dynamicObject));
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException |
NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
}
}

You might also like