HOME FORUM BLOGS CONTACT LINKS FEEDBACK LOGIN REGISTER
 

JAVA DEVELOPER SITE - COMPLETE JAVA TUTORIALS

Invoke Method using Java reflection API



Newly Added TutorialNewly Added Tutorial


Java Exception Handling
Factory Design Pattern
Thread Creation in Java
Read a File in Java
Convert List to Array
Read Excel in Java
Setting thread priorities
Method Invocation Using Reflection
Ajax Basics
Stateless Session Beans Callback
Ajax Autocomplete in Java
Hello World Java
Hello World Java
Decision Table
Object Serialization
Singleton Pattern
Java Heap Size
Set Heap Size Eclipse
OutofMemory Exception in Java
Axis Web Services
Java C++ Difference
SOA Tutorials
Filenet Platform BPM
Eclipse Tips & Tricks
enterprise service bus (ESB)
Enterprise Application Integration (EAI)

JAVA Tutorials

Collection Framework
Operators in Java
If-else Statement

Thread
File
Generics
Input-Output in java
File Stream
Java Methods
Java Arrays
Java Objects
Java Exception Handling
Java Inheritance Tutorial
Java Threads
Garbage collection

J2EE Tutorials

Servlets Tutorials
Introduction to Struts Framework
JSP Tutorials
SPRING Framework Tutorials
STRUTS Tutorials
HIBERNATE Tutorials
WEB-SERVICES Tutorials

Java Collections

MAP
SET
LIST
VECTOR

Advanced Java

Stateless Session Bean
Introduction to EJB
Stateful Session Bean
Java Design Patterns

BRMS Tutorial

ILOG
BLAZE ADVISOR
PEGA
Drools
BRMS
BPM
JRules for .NET
ilog jrules interview questions
ilog jrules certification dumps
Java Certification Study Guide
hibernate tutorial
hello world struts example

All Tutorial

php mysql tutorial
SCJP 1.5/1.6
SCWCD 1.5/1.6
Polymorphism
Thread Tutorial
Generics & Exceptions
Spring Struts Hibernate

 
 
 

Useful Java Links




This java code example shows how to invoke methods on an object at runtime without knowing the names of them in advance.
This is possible using the reflection API. What we do is to get an instance of the class object of the particular class we want to call methods on and by using that instance we can get the array of Method objects by calling getDeclaredMethods().
We dont actually call the methods of the class instance, instead we have to create an object that we call the methods on by sending it as an argument to the invoke() method.
In this example we use an inner class named "Computer" but of course it could be any object, not necessarily an instance of an inner class. The names of the methods invoked are printed out along with the values that they return.

Code Examples For Java Reflection API

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

/**
 *
 * @author javadb.com
 */

public class Main {
    
    /**
     * Invokes the methods of an object using the Reflection api.
     */

    public void invokeMethodsUsingReflection() {

        //Obtain the Class instance
        Class computerClass = Computer.class;
        
        //Get the methods
        Method[] methods = computerClass.getDeclaredMethods();
        
        //Create the object that we want to invoke the methods on
        Computer computer = new Computer();
        
        //Loop through the methods and invoke them
        for (Method method : methods) {
            Object result;
            try {
                //Call the method. Since none of them takes arguments we just
                //pass an empty array as second parameter.
                result = method.invoke(computer, new Object[0]);
            } catch (IllegalArgumentException ex) {
                ex.printStackTrace();
                return;
            } catch (InvocationTargetException ex) {
                ex.printStackTrace();
                return;
            } catch (IllegalAccessException ex) {
                ex.printStackTrace();
                return;
            }
            System.out.println(method.getName() + ": " + result);
        }

/**
     * @param args the command line arguments
     */

    public static void main(String[] args) {
        new Main().invokeMethodsUsingReflection();
    }
    
    
    class Computer {
        
        private String brand = "DELL";
        private String type = "Laptop";
        private int harddiskSize_GB = 300;
        private boolean AntiVirusInstalled = true;

        public String getBrand() {
            return brand;
        }

        public String getType() {
            return type;
        }

        public int getHarddiskSize_GB() {
            return harddiskSize_GB;
        }

        public boolean isAntiVirusInstalled() {
            return AntiVirusInstalled;
        }
    }
}



If you need any urgent assistance on Java_reflection_API_Tutorial, kindly email your requirement to us at : info@javagenious.com or Contact-an-Expert. Our experts will try their best to solve your problem.

Keyword Tags: Java_reflection_API_Tutorial tutorial,Java_reflection_API_Tutorial in java,Concepts of Java_reflection_API_Tutorial,Java,J2EE,Interview Questions,Java_reflection_API_Tutorial Examples


Java Virtual Machine (JVM)

Most programming languages compile source code directly into machine code, suitable for execution on a particular microprocessor architecture. read more


Java Related Discussions

 
Javagenious.com

Interview Questions
Placement Papers
FAQs on J2EE
FAQs on JNDI
XML-Tutorials
Remote Web Services
Exception-Handling
JMS Tutorials
seam richfaces tutorial
richfaces live demo
jquery example demo
JQuery Tutorial and Source Code
JSF Tutorial
XSL Tutorial
J2EE JMS
CORBA Applications
Java CERTIFICATION
SCJP 1.5
SCJP 1.6
SCWCD 1.5
SCBCD
Mis
Ajax autosuggest autocomplete from database
Struts forms and validation with webflow
Java Apache POI Examples
JDBC Tutorial

Oracle
MYSQL Tutorial
php Tutorial
JQuery Examples
JQuery Tuorial
Upload Java File
JAXB Examples
PL-SQL Tutorial
Dojo AJAX
EJB Examples
Java IDE
Eclipse
NetBeans
IBM Rational Weblogic Workshop
Downloads
SCJP 1.6 Dumps
SCJP 1.5 DUMP
SCJP Material
SCWCD
Java Security
SCBCD Dumps
External Links
SUN Java
JOracle
Bea Weblogic
IBM
PHP Certification
Technical Interview Questions
Amazon Interview Questions
Google Interview Questions
Microsoft Interview Questions
IBM Interview Questions
Yahoo Interview Questions
HOME | FORUM | ABOUT | BLOGS | CONTACT-US