Skip to main content

Invoking Class Methods using Reflection in Java

Sometimes we want the code should be dynamic and we even want to call the class methods dynamically. This article describes on how we can invoke the class methods dynamically using variables.

To achieve the above operation we use the concept of Reflection in Java. Here in this article i am using 2 java files, Client.java and Server.java. Server.java will be the class that we want to load dynamically and Client.java will have the code responsible for calling the Server class dynamically.
Server.java:
Client.java:
import java.lang.reflect.Method;

public class Client {
  public static void main(String args[]) {
     try {
        Class dynamicClass = null;
        dynamicClass = Class.forName("Server");

        //Loading methods parameters
        Class[] parameter=new Class[1];
        parameter[0]=int.class;

        Object iClass = dynamicClass.newInstance();

        //Loading and Invoking setPort method with argument passed 
        Method thisMethod = dynamicClass.getMethod("setPort", parameter);
        thisMethod.invoke(iClass, 6000);

        //Loading and invoking getPort method withour argument passed, this method also returns int value
        Method thisMethod1 = dynamicClass.getMethod("getPort", null);
  int data = (Integer) thisMethod1.invoke(iClass);

        System.out.println(data); 

     } catch (ClassNotFoundException e) {
    e.printStackTrace();
     } catch (SecurityException e) {
          e.printStackTrace();
     } catch (InstantiationException e) {
          e.printStackTrace();
     } catch (IllegalAccessException e) {
          e.printStackTrace();
     } catch (IllegalArgumentException e) {
          e.printStackTrace();
     } catch (Exception e) {
          e.printStackTrace();
     }
  }
}
public class Server {
   private int PORT = 0;

   public Server() {
 System.out.println("Inside Server Class");
   }

   public void setPort(int portno) {
       this.PORT = portno;
   }

   public int getPort() {
       return this.PORT;
   }
}
This class is basically an bean structure having getter and setter methods.

Here if you see i am initially loading the Server.class dynamially, than i am calling setPort() method and passing 6000 value as an argument. Than i am reading the port value set.

Comments

Popular posts from this blog

The Bourne Betrayal | Book Review

Novel by Eric Van Lustbader and Robert Ludlum I like all Robert Ludlum’s novels including those which are written by Evan Lastbadder. To me his novels have taken fiction to the next level. During my way back to Hyderabad from my last summer trip to hometown I bought paperback version of “The Bourne Betryal”. This novel was full of Lastbadder’s style of writing than Robert Ludlum’s one.  I took almost 6 months to complete it. This novel has something different to offer actually. Plot is exciting but the story is not very accelerating. Jason bourn and Martin Lindros, When martin Lindros decided to come back in the field operations with the aim to destroy Fadi and When Martin is out the track , Jason is the only help possible in the situation. Story takes you through various struggle of Jason to bring Martin back home. There are few things where author has not even paid any attention for example how does an ordinary Pakistani Waiter will have that much of information   Towa...

New Programming Language, Do we really need ?

We have seen multiple new programming languages every year and question which is commonly asked is Will there always be new programming languages coming out? I believe  many new programming languages will keep coming and many are on the way. Technologies are evolving around us will make it fairly easy; everyday big corporation and tech individual/communities are making consistent progress towards technological advancement. If you observe every new programming language has one thing in common and that is they are based on software engineering principals, you will find same loop, iteration, conditional processing and stuff like that. What makes them different is Adoption, you can make your own programming language using other languages but it will only be popular when it can convince large tech community on the ground of Efficiency, Security, Agility, Portability, platform support etc. Wikipedia has impressive List of programming languages We have seen many languages in the pa...

ipconfig/displaydns

Why does the aboave command prints URLs, Websites addresses that we have never accessed before ? When your system communicates with the DNS server for resolvingthe name queries, Your system builds the cache over the perios of time, This cache normally contains records from the host file and also the retrieved records from the recently resolved queries. Coming to the question that the site which were never accessed showing up there. DNS cache notes down positive and negative results as well. as you know caching is all about performance improvment. Now lets say you accessed Website1 and Website1 has some functionalities which makes it to communicate with Website2. Now Fortunately or unfortunately Website2 is blocked in your network and name query for this Website is not resolved. Still this unresolved queries will be recorded in DNS cache. I think those results are coming as they were initiated from your system implicitly. Issue this command to clean the DNS cache