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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:56:03+00:00 2026-05-30T21:56:03+00:00

I want an interface, which its sub class can inherit a static field, that

  • 0

I want an interface, which its sub class can inherit a static field, that field points to the name of the sub class.

How can I do that?

For example in my mind (the code is unusable):

public interface ILogger<A> {

    public static String LogTag = A.class.getName();
}

public class Sub implements ILogger<Sub> {

    public Sub() {
        Log.debug(LogTag, ...);
    }
}
  • 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-30T21:56:04+00:00Added an answer on May 30, 2026 at 9:56 pm

    In Java, unlike C++, this is not possible due to the way that generics are implemented. In Java, there is only one class for each generic type, not multiple copies for each time a different type argument is used (this is called erasure). As a result, you can’t have a single variable that points to the class object of its subtype, because there can be many subtypes but there is always exactly one copy of the static field. This contrasts with C++, where each time the ILogger template is instantiated you would get your own copy of that static field.

    One possible approximation would be to have a Map as a static field that associates class objects with strings, as in

    public static final Map<Class, String> classMap = new HashMap<Class, String>();
    

    You would then have to have each subtype explicitly add itself to this map, perhaps with a static initializer:

    public class Sub implements ILogger<Sub> {
    
        static {
            ILogger.classMap.put(Sub.class, /* ... value ... */);
        }
    
        public Sub() {
            Log.debug(LogTag, ...);
        }
    }
    

    Hope this helps!

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

Sidebar

Related Questions

I want to have a class which implements an interface, which specifies the specific
I want to programaticly retrieve the Interface ID for any class so that I
I want to make an interface in C# that defines a method that always
I want to create search interface to search data from GAE datastore? Can i
I'm trying to create a sub-class of the JButton component that will enable or
So I have an UIImageView which can be rotated about its center with touchesMoved.
I want to provide a telnet interface to my Java application, primarily so that
I am trying to make a abstract ObservableCollection class which requires its members to
I have five textboxes which are associated with its own field from the my
I've got a polymorphic array of objects which implement two (informal) interfaces. I want

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.