-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathDemo1.java
More file actions
52 lines (48 loc) · 1.54 KB
/
Demo1.java
File metadata and controls
52 lines (48 loc) · 1.54 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.io.FileNotFoundException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;
class Calc
{
private int add(int x , int y){
return x + y;
}
private int subtract(int x , int y){
return x - y;
}
Calc(){
System.out.println("Calc Cons Call");
}
static{
System.out.println("Calc Class Loaded...");
}
}
class ScCalc{
ScCalc(){
System.out.println("Sc Calc Cons Call");
}
static{
System.out.println("Sc Calc Class Loaded...");
}
}
public class Demo1 {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, FileNotFoundException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException {
// TODO Auto-generated method stub
// Eager Way
//Calc calc = new Calc(); // Static Coding
Scanner scanner = new Scanner(System.in);
//System.setOut(new PrintStream("/Users/amit/Documents/FileHandlingTesting/abcd.txt"));
System.out.println("Enter the Class Name ");
String className = scanner.next();
System.out.println("Enter the Operation Name");
String operationName = scanner.next();
// Dynamic Coding
//Class.forName(className); // this is dynamic class loading
// Dynamic Object Creation
Object object = Class.forName(className).newInstance();
Method method =object.getClass().getDeclaredMethod(operationName, int.class,int.class);
method.setAccessible(true);
Object result = method.invoke(object, 100,200);
System.out.println("Result is "+result);
}
}