Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8353297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:12:51+00:00 2026-06-09T09:12:51+00:00

I have been looking at Reflections lately in java and I have come up

  • 0

I have been looking at Reflections lately in java and I have come up with a simple example

Reflection.java

 import java.lang.reflect.*;

  public class Reflection{
  }

  public static void main(String args[])
  {
     try {
       Class cls = Class.forName("Method");

        Method methlist[] 
          = cls.getDeclaredMethods();
        for (int i = 0; i < methlist.length;
           i++) {  
           Method m = methlist[i];
           System.out.println("name 
             = " + m.getName());
           System.out.println("decl class = " +
                          m.getDeclaringClass());
           Class pvec[] = m.getParameterTypes();
           for (int j = 0; j < pvec.length; j++)
              System.out.println("
               param #" + j + " " + pvec[j]);
           Class evec[] = m.getExceptionTypes();
           for (int j = 0; j < evec.length; j++)
              System.out.println("exc #" + j 
                + " " + evec[j]);
           System.out.println("return type = " +
                              m.getReturnType());
           System.out.println("-----");
        }
     }
     catch (Throwable e) {
        System.err.println(e);
     }
  }
 }

Method.java

 public class Method {
  private int f1(
   Object p, int x) throws NullPointerException
  {
     if (p == null)
        throw new NullPointerException();
     return x;
  }

From the above example i am able to return all the information from Method.java which looks like this

name = f1
decl class = class Method
param #0 class java.lang.Object
param #1 int
exc #0 class java.lang.NullPointerException
return type = int
-----
name = main
decl class = class method1
param #0 class [Ljava.lang.String;
return type = void
-----

But my question is, is there a way to test Method.java from Reflection.java with something like

int x = cls.getMethod("f1", Object = anObject, Integer.TYPE = 2 )
System.out.println(x);

And the console should print out 2 because f1 was passed an object?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:12:52+00:00Added an answer on June 9, 2026 at 9:12 am

    You need to create an instance of Method and invoke the private method f1 with the two parameters. It will return an object. There are a couple of pitfalls in your code, but here’s a working solution (Note: I renamed your class Method to MyMethod to avoid a name clash:

    Method f1Method = MyMethod.class.getDeclaredMethod("f1", Object.class, int.class);
    f1Method.setAccessible(true);
    int result = (Integer) f1Method.invoke(new MyMethod(), 
                               new Object[]{new Object(), 2});
    System.out.println(result);
    
    • The method is private so you need to get the declared method and make it accessible
    • The method is not static so we need an instance of MyMethod to invoke the method
    • We pass and receive a java primitive type. int.class is allowed. JavaDoc tells us, that if the value has a primitive type, it is first appropriately wrapped in an object. So we’ll receive an instance of Integer.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been looking at Reflection.Emit recently. I wrote a simple program that generates
I have been looking through the source code for the Flash-based Google TV example
I have been looking at how to reflect a point in a line, and
Have been looking on some tutorials for drawing canvas using SurfaceView, but the only
I have been looking around for a visualization framework that would aid graph visualization
I have been looking online to find documentation for the function in QTP and
I have been looking into AWS spot instances for some jobs however instead of
I have been looking into backbone.js and I can't seem to figure out how
I have been looking at the Thinktecture.IdentityModel.40 library as a way of handling the
I have been looking for an answer to this on Stack Overflow, but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.