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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:48:47+00:00 2026-06-03T05:48:47+00:00

Sometimes it is useful to get the type name from static invoke, see this

  • 0

Sometimes it is useful to get the type name from static invoke, see this example:

class Base {
    private static void funImpl(Class<? extends Base> type) {
        ...
    }
    public static void fun() {
        fun(Base.class);
    }
}

class User extends Base {
    public static void fun() {
        fun(User.class);
    }
}

Here fun() is declared for each subclass. So, can I write something like this?

class Base {
     private static void funImpl(Class<? extends Base> type) {
         ...
     }
     public static void fun() {
         Class<?> leftHandClass = System.getLeftHandClass(); // Error: d'oh...
         funImpl(leftHandClass);
     }
}

class User extends Base {
}
  • 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-03T05:48:48+00:00Added an answer on June 3, 2026 at 5:48 am

    Agreed. Rather than the superclass having to be “told” what subclass is using it, you do it the other way around and declare abstract methods in the superclass for the subclasses to implement. That way, instead of having one big class that has a bunch of case statements to handle each of its subclasses, the code specific to each subclass goes in the subclass.

    If your problem is that you want your subclass instances to be singletons (i.e. so as to use a static method), consider using an enumeration. So rather than calling a static method on a class, you call an instance method on the enumeration constant: SQUARE.fun() etc.

    enum Foo {
        SQUARE {
            public String getColor() {
                return "RED";
            }
        },
        CIRCLE {
            public String getColor() {
                return "GREEN";
            }
        },
        TRIANGLE {
            public String getColor() {
                return "BLUE";
            }
        };
        public abstract String getColor();
    
        public void fun() {
            System.out.println("I am a " + name() + " and my color is " + getColor());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sometimes it's useful to derive my entities from a base class like this: public
I get this error sometimes, Catchable fatal error: Object of class stdClass could not
Sometimes it would be useful to name variables like no programmer should name his
Sometimes, it is useful to hide a string from a binary (executable) file. For
I find it useful sometimes to be able to make a local module (inside
Sometimes it is useful to enumerate a list while it is changing. e.g. foreach
Sometimes it might be useful, but mostly just looking cool or impressive to visualize
Memcached interface is implemented using a textual protocol. Sometimes it's very useful to be
Sometimes it is useful to use the starting address of an std::vector and temporarily
For one reason or another, I sometimes find it useful or just interesting to

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.