473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,385 developers and data experts.

How to use Reflection in Java

sreekandank
Reflection is the ability of java program which allows to analyze itself. The java.lang.reflect package provides the ability to obtain the information about the fields, constructors, methods and modifiers of a class.
A program that can analyze the capabilities of classes is called reflective.

The following are the four features of reflection:
  • Analyze the capabilities of class at run time
  • Analyze objects at run time
  • Write generic array code
  • Method pointers
The following are the important classes available in java.lang.reflect package.
  1. Array
  2. Field
  3. Constructor
  4. Method
  5. Modifier
These classes are used to analyze about the fields, constructors, methods and modifiers.

The following simple program illustrates the concept of reflection.

ReflectionDemo.java
Expand|Select|Wrap|Line Numbers
  1. import java.lang.reflect.*;
  2. /**
  3.  *@author Sreekandan.K
  4.  */
  5. public class ReflectionDemo
  6. {
  7.  public static void main(String args[])
  8.  {
  9.   try
  10.   {
  11.    String clsName="java.util.Date";
  12.    Class c=Class.forName(clsName);
  13.    System.out.println("Constructors...");
  14.    Constructor cnts[]=c.getConstructors();
  15.    for(int i=0;i<cnts.length;i++)
  16.    {
  17.     System.out.println(" "+cnts[i]);
  18.    }
  19.    System.out.println("Fields...");
  20.    Field flds[]=c.getFields();
  21.    for(int i=0;i<flds.length;i++)
  22.    {
  23.     System.out.println(" "+flds[i]);
  24.    }
  25.    System.out.println("Methods...");
  26.    Method mds[]=c.getMethods();
  27.    for(int i=0;i<mds.length;i++)
  28.    {
  29.     System.out.println(" "+mds[i]);
  30.    }
  31.   }
  32.   catch(Exception e)
  33.   {
  34.    System.out.println("Exception:"+e);
  35.   }
  36.  }
  37. }
Oct 26 '11 #1
0 4586

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Fu Bo Xia | last post by:
the java.lang.Object.forName method takes a java class name and returns a Class object associated with that class. eg. Class myClass = Object.forName("java.lang.String"); by if i only know the...
0
by: ws_dev2001 | last post by:
Hello all, I am trying to obtain the size of a java object in C by using JNI. As we do not have a proper implementation of this in java, I decided to see if C could provide me some accurate data....
3
by: Lucia | last post by:
Hello, I'd like store all constants in my java-Application in an XML-file. e.g. <constant> <name>con</name> <type>long</type> <value>10</value> </constant>
13
by: Moonraker | last post by:
After working extensively with .Net and Java, these are the differences I see: Java does not have Reflection.Emit But it does have Reflection. That's okay so far, because most programmers are...
21
by: Martin Demberger | last post by:
Hi, I'm working on an article about compiling java-sources to c++. I'm interessted if someone would use this to write a program in java (and the good IDE's available for java) and convert this to...
7
by: sujit | last post by:
Hi All, I have query regarding "C++ and Reflection". I could not get sufficient information from net on this topic. Does C++ support reflection or any features similar to some other languages...
2
by: ws_dev2001 | last post by:
Hello all, I am trying to obtain the size of a java object in C by using JNI. As we do not have a proper implementation of this in java, I decided to see if C could provide me some accurate data....
0
by: ranadhirnag | last post by:
I need to check a property in the javax.faces.component.html.HtmlInputText. If i do it in the following manner,I get things fine:...
9
by: Kuberan Naganathan | last post by:
Hello all Does anyone know of a good way to use reflection in c++? I don't mean simply using rtti or dynamic casting. I'm talking about java/c# style reflection where an actual instance of...
7
by: =?Utf-8?B?TXJOb2JvZHk=?= | last post by:
Say I have a class that has a generics List as follows: public List<MyClassmyClassList = new List<MyClass>(); and I want to create another class which tries to add an element of MyClass to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.