à®®ேலுà®®் பல புதிய டெக்னாலஜி பற்à®±ி தெà®°ிந்து கொள்ள, எங்கள் "டெக் டுப் தமிà®´் நாடு" யூட் டூஃப் சேனலை SUBSCRIBE செய்து கொள்ளவுà®®்.

Breaking

Sunday, 8 October 2017

Reflection API in JAVA 2


Reflection API in JAVA 2

java.lang.Class class

Class is a final class in java.lang package which extends Object class. Instance of this class represents classes and interfaces in a running Java application. It is used to analyze and change dynamic behaviour of a class at runtime.

Some Important Methods of java.lang.Class class

This class defines several methods using which we can get information about methods, constructors, modifiers and members of a class at runtime.

forName( )

This method takes fully qualified name of classes or interface as its argument and returns instance of the class assocaited with it.

Syntax

static Class< ?> forName(String className)

Example using forName() method

class Student{}
class Test
{
 public static void main(String args[])
 {
  Class c = Class.forName("Student");
  System.out.println(c.getName());
 }
}

Output : Student

getConstructors( ) and getDeclaredConstructors( )

getConstructors() method returns array of Constructors object that represent all the public constructors of the invoking object. Remember, this method only returns public constructors. If you want to see all the declared constructors of a class then use getDeclaredConstructors(). Following is the general syntax of both,

Constructor< ?>[] getConstructors();
Constructor< ?>[] getDeclaredConstructors();

Example using getConstructors() and getDeclaredConstructors() method

import java.lang.reflect.*;
class Student
{
 public Student(){}
 public Student(String name){}
}

class Test
{
 public static void main(String args[])
 {
  try
  {
  Class c = Class.forName("Student");
  Constructor< Student>[] ct = c.getConstructors();
  for(int i=0; i< ct.length; i++)
    { System.out.println(ct[i]); }
  Constructor< Student>[] cdt = c.getDeclaredConstructors();
  for(int i=0;i< cdt.length;i++)
    { System.out.println(cdt[i]);}
  }
  catch(Exception e)
  { e.printStackTrace();}
 }
}

Output :
public Student()
public Student()
Student(java.lang.String)

getMethods( ) and getDeclaredMethods( )

getMethods() method returns array of Method object that reflect all the public method of invoking object. getDeclaredMethods() returns only the declared methods of the invoking class object.

Syntax for both is following,

Method< ?>[] getMethods();
Method< ?>[] getDeclaredMethods();

Example using getDeclaredMethods() method

import java.lang.reflect.*;
class Student
{
 public void show(){}
 void display(){}
}

class Test
{
 public static void main(String args[])
 {
  try
  {
   Class c = Class.forName("Student");
   Method md[] = c.getDeclaredMethods();
   for(int i=0; i< md.length; i++ )
    { System.out.println(md[i]); }
  }
  catch(Exception e)
  { e.printStackTrace();}
 }
}

Output :
public void Student.show()
void Student.display()

getFields( ) and getDeclaredFields( )

getFields() returns an array containing Field objects reflecting all the accessible public members of the class or interface represented by this Class object. getDeclaredFields() returns array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.

Field< ?>[] getFields();
Field< ?>[] getDeclaredFields();

Example using getFields() and getDeclaredFields() method

import java.lang.reflect.*;
class Student
{
 public  String name;
 int roll;
}

class Test
{
 public static void main(String args[])
 {
  try
  {
   Class c = Class.forName("Student");
   Field ff[] = c.getFields();
   for(int i=0; i< ff.length; i++)
    { System.out.println(ff[i]); }
   Field f[] = c.getDeclaredFields();
   for(int i=0;i< f.length; i++)
    { System.out.println(f[i]);  }
  }
  catch(Exception e)
  { e.printStackTrace();}
 }
}

Output :
public java.lang.String Student.name
public java.lang.String Student.name
int Student.roll
  

No comments:

Post a Comment

Hai , Post your comment . (required, Bugs, Errors )

You need to see


SUBSCRIBE NOW



Backlinks : Website Design Company in Coimbatore
Website Design Company in Tamil nadu
Website Design Company in Karur
Website Design Company in Erode
Website Design Company in ooty
Website Design Company in theni
Website Design Company in thoothukudi
Website Design Company in Chennai
Website Design Company in Sathy
Website Design Company in Coimbatore Tamilnadu
Cheap Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
ecommerce Website Design Company in Coimbatore
school Website Design Company in Coimbatore
Academy Website Design Company in Coimbatore
Test Website Design Company in Coimbatore
Best Website Design Company in Coimbatore
Adsense Website Design Company in Coimbatore
Adsense Website Design Company in Tamil nadu
Adsense Website Design Company in Karur
Adsense Website Design Company in Erode
Adsense Website Design Company in ooty
Adsense Website Design Company in theni
Adsense Website Design Company in thoothukudi
Adsense Website Design Company in Chennai
Adsense Website Design Company in Sathy
Free SEO Tools online
Free SEO Tools online Website
Free SEO Tools online Plagiarism
Free SEO Tools online article rewriter tool
Free SEO Tools online article reading and writing criticism
Free SEO Tools online seo backlink
Free SEO Tools online Free backlink maker
Free SEO Tools online seo backlink strategy
Free SEO Tools online content writting
360 help SEO Tools online Plagiarism checker
360 help SEO Tools online seo backlink
360 help SEO Tools online Free backlink maker
360 help SEO Tools online content writting
360 help SEO Tools online article rewriter tool
360 help SEO Tools
360 help SEO Tools India
360 help SEO Tools Tamil
360 help SEO Tools website