Operators in java:
Assignment operators: =
Concatenation operator: +
Arithmetic operators: + , - , * , /
Comparison operator: <,>,<=,>=,==,!=
Logical operator: &&,||,!
Ternary operator: ?:
Ternary operator:
it is an alternative for if else.
Syntax: condition ? trueresult : falseresult ;
Eg: String av="sree",ev="sree";
System.out.println(ev.equals(av)
? "passed" :"failed");
Reflection API: it is a set of predefined classes, among them
one of the classes which is useful for us is Method
Method class reference can hold any method
(function) in the specified class using the same reference we can invoke that
method (function).
Syntax1: String s="login"; // here login is a
method having no arguments
Method
m=methodreflectionapi.class.getMethod(s);
m.invoke(m);
syntax2: String s1="add"; // here add is a
method having two arguments of type Integer
Method
m1=methodreflectionapi.class.getMethod(s1,Integer.class,Integer.class);
m1.invoke(m1,10,20);
Note1:
here if you use m.invoke(m); then it will call any method having no arguments,
we can do it by just changing the String s value to another method name
Note2: here if you use m1.invoke(m1,”sree”,”kola”); then it will
call any method having two string arguments, we can do it by just changing the
String s value to another method name having two arguments.
Here methodreflectionapi is the class
in which you have created and used to write Method
Note: from the above
note1 and note2 what we need to understand is
If at all a group of methods having same no of
arguments then we can go for one reflectionapi’s Method class object for all
the methods to avoid switch case. Using that object we can invoke any method as
u like.
If at all another group of methods having
different no of arguments then we can go for that many no reflectionapi’s
method class objects for avoiding switch case. Using those objects we can
invoke corresponding method based on the no of parameters. So this is one back
drop from this concept.
It is working fine for static methods only.
1. Method
Example 1 for Method:
public class ex {
public static void login(){
System.out.println("logic for login");
}
public static void ba(){
System.out.println("logic for ba");
}
public static void logout(){
System.out.println("logic for logout");
}
}
Main function()
String str="login";
Method m=ex.class.getMethod(str);
m.invoke(m);
Example 2 for Method:
public class ex {
{
System.out.println("logic for login"+name+-----+lname);
}
public static void ba(){
System.out.println("logic for ba");
}
public static void logout(){
System.out.println("logic for logout");
}
}
public class test {
public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
m.invoke(m,"AKHIL","REDDY");
}
}
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class xx {
public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
String str="login";
Method m=refapi.class.getMethod(str,String.class,String.class);
m.invoke(m,"AKHIL","REDDY");
String str1="login";
Method m1=refapi.class.getMethod(str1);
m1.invoke(m);
Method m2=refapi.class.getMethod(str,String.class,String.class);
m2.invoke(m,"100","200");
}
}
Different datatypes
Api
public static void stradd(String a,String b)
{
System.out.println("string is "+ a + b);
}
public static void add(int a,int b)
{
int c=a + b;
System.out.println("sum is "+c);
}
public static void stradd1(String a,int b)
{
System.out.println("string is "+ a + b);
}
Reflection api
String s="stradd";
Method
m1=api.class.getMethod(s,String.class,String.class);
m1.invoke(m1,"sree","akki");
String s1="add";
Method
m2=api.class.getMethod(s1,int.class,int.class);
m2.invoke(m2,10,20);
String s2="stradd1";
Method m3=api.class.getMethod(s2,String.class,int.class);
m3.invoke(m3,"sree",10);
No comments:
Post a Comment