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 8779419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:46:55+00:00 2026-06-13T19:46:55+00:00

Ok I do understand how Java Reflection works. But what I am doing is

  • 0

Ok I do understand how Java Reflection works. But what I am doing is kinda different from what is shown on the Reflection tutorials. Now what I want below is to invoke a method that is returned by a invoking a method by using reflection.

   class Foo{
          private String str = "";

          public Foo(String str){
              str = this.str;
          }

          public void goo(){
            System.out.println(this.str);
          }
    }

    class Bar{
         public Foo met(String str){
              return new Foo(str); 
         }
    }

    class Hee{
         public static void main(String [] args) throws Exception{
                Class cls = Class.forName("Bar");
                Object obj = cls.newInstance();

                Class [] types = {String.class};
                String [] arr = {"hello"};
                Method method = cls.getMethod("met",types);

        Object target = method.invoke(obj, arr);

                target.goo(); //here where the error occurs
                // 123456
        }
    }

Now, I depend to much on my experience that my method.invoke() would return the object that is being returned by the method that is being returned by the method that is reflected. But seems it doesn’t work.. I debugged my code ans seems it doesn’t return anything. What I doing wrong? Please tell me if I did something wrong

  • 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-13T19:46:56+00:00Added an answer on June 13, 2026 at 7:46 pm

    Probably need to cast target object to foo type.

    ((foo)target).goo();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found some posts about java/reflection on this site. But still can't understand something.
I'm trying to understand how java.util.Calendar.get(java.util.Calendar.WEEK_OF_YEAR) works, but it seems that I'm missing some
I am reading the Java Hashmap documentation but I don't understand this sentence. Note
I am trying to understand this example from Thinking in Java : package c07;
I want to flatten/transform an instance from a given Java class to another instance
I'm doing some exercise to understand Java and Swing API. Why do I have
I read many articles to understand Java servlet but I did not succeed. Can
From what I understand Java packages often use a company's website as a package
Quick edit: As was pointed out below I should be doing a BFS but
In the process of doing some reflection on Java types, I came across an

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.