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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:08:45+00:00 2026-05-30T02:08:45+00:00

Is there some way of using magic methods in Java like there is in

  • 0

Is there some way of using magic methods in Java like there is in PHP with __call?

For instance:

 class foo {
     @Setter @Getter
     int id;

     @Getter
     Map <String, ClassInFoo> myMap;

     protected class ClassInFoo {
          @Setter @Getter
          String name;
     }

     @Setter
     String defaultKey;
 }

I’m using Project Lombok annotations for getter and setter methods to simplify the code.

Let’s consider that that my map contains several items mapped by String and the defaultKey defines the default one.

What I would like is to be able to call foo.getName() which would return the default name as foo.myMap.get(defaultKey).getName().

The reason I can’t just write all the getters manually is that the Foo class is in fact inherited with generics and the the inner class might be different.

I sort of need something like:

   function Object __call(method) {
           if (exist_method(this.method)
                return this.method();
           else 
                return this.myMap.get(defaultKey).method();
   }

Is this somehow possible in Java?

EDIT:

I made a more precise example of what I am trying to achieve here: https://gist.github.com/1864457

The only reason of doing this is to “shorthand” the methods in the inner class.

  • 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-05-30T02:08:45+00:00Added an answer on May 30, 2026 at 2:08 am

    You absolutely can through reflection by using its features like

    public Method getMethod(String name, Class<?>... parameterTypes)
    

    that can be used to see if a class has some methods defined but I don’t see how your problem couldn’t be solved with a proper use of interfaces, inheritance and overriding of methods

    Features like reflection are provided to manage certain, otherwise unsolvable, issues but Java is not PHP so you should try to avoid using it when possible, since it’s not in the philosophy of the language.

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

Sidebar

Related Questions

Is there some way to block access from a referrer using a .htaccess file
Is there some way I can define String[int] to avoid using String.CharAt(int) ?
I'm using symfony 1.4.3 Is there some way to render a sfWidgetFormChoice as an
Is there a way to add some custom font on a website without using
Is there a way to get a file's MIME type using some system call
Is there some way I can use URLs like: http://www.blog.com/team-spirit/ instead of http://www.blog.com/?p=122 in
Instead of using $this->fetchAll('email = ?',$email)->current() inside the model class, is there a way
Is there some way of using OpenCV's imread function to read from std::in ?
Let's assume I have an annotated bean property setter like this: public class Foo
I am using the __call magic within some of my mvc code to produce

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.