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

  • Home
  • SEARCH
  • 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 8133935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:49:13+00:00 2026-06-06T09:49:13+00:00

I have spend the last 1 or 2 hours diving into Java reflection. I

  • 0

I have spend the last 1 or 2 hours diving into Java reflection.
I think i’m starting to have a proper understanding.

I could not however find a few answers I was looking for.

What I came to understand is that reflection suffers a big(biggest?) performance hit from class lookups.

I have 2 questions.

How can you call a method from within the current context(is it even possible?)?
And will the performance hit from class lookups be canceled out when calling within the current context?

for example:

class User {
   private String name;

   public getName(){ return name; }
   public setName(String name){ this.name = name; }

   public void doSomething() {
       //some random stuff
       //I would like this method to invoke randomMethod();
       //Since it is within the same context(this)
       //Will this reduce the performance cost?

       //Please assume from my goals that I will ALWAYS know the name of the method i                  want to call.
      //So I wont need to loop through all available methods.

   }

   public void randomMethod() {

   }
}

Im trying to achieve some kind of dispatcher.
For example for web development in Java.

Im not interested in frameworks etc.

So if a user enters an url http://www.hiurl.com/home/index

Where home is the controller and index the action(the method name to be called by reflection).

If you have good arguments why to absolutely avoid this apart from the many chances of failing, please let me know aswell.

I hope my question is clear.
Thanks for taking the time to read and I look forward to read your replies.

  • 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-06T09:49:14+00:00Added an answer on June 6, 2026 at 9:49 am

    No, unfortunately one cannot optimize subsequent method calls via reflection even if all of them are executed on the same instance. The reason for this basically is the signature of reflection methods for invocation:

    // in java.lang.reflect.Method
    Object invoke(Object instance, Object... args);
    
    // in java.lang.reflect.Field
    Object get(Object instance)
    

    The only thing one can do to optimize calls using reflection is to store references to Method, Field, Constructor etc. in order to avoid costly lookups each invocation e.g:

    public class User {
    
        public void methodToBeCalledUsingReflection() {
            // some logic
        }
    
        public void invocationWithPerformanceHit() {
            // lookup of Method instance - costly operation!
            Method method = User.class.getMethod("methodToBeCalledUsingReflection");
            // actual invocation of method
            method.invoke(this);
        }
    
        public void invocationWithoutPerformanceHit() {
            // only actual invocation of method
            method.invoke(this);
        }
    
        // moving Method instance to static field which is initialized (looked up) only once
        public static final Method method = getMethodReference("methodToBeCalledUsingReflection");
    
        private static Method getMethodReference(String methodName) {
            try {
                return User.class.getMethod(methodName);
            } catch(Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    }
    

    Besides that, I would recommend using Reflection only if it’s highly justified since it hampers performance, is less typesafe and has some other drawbacks. If one can go without Reflection, one should not use it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have spent the last 4 hours trying to get my iPad to run
I have spent the last 3 hours trying to get this code working. I
I have spent the last couple of hours battling to write some javascript and
Urgh, I have spent the last couple of hours on this now. I normally
I have spent the last several hours trying to get this to work and
I have spent the last few hours putting togeather the following code after reading
I have spent last couple of hours searching for answers here but nothing seems
I have spent the last several hours scouring the internet looking for examples and
I know this is an old question, but I have spend any hours on
I have spent the last hour trying to see what I'm doing wrong, or

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.