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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:32:32+00:00 2026-06-15T10:32:32+00:00

This seems like a newbish question, but the last time I worked with Java,

  • 0

This seems like a newbish question, but the last time I worked with Java, the language didn’t have generics. I have a class hierarchy (names changed to be as generalized as possible):

public abstract class AbstractBase { .... }
public class ConcreateSubA extends AbstractBase { .... }
public class ConcreateSubB extends AbstractBase { .... }
...
public class ConcreateSubZZ9PluralZAlpha extends AbstractBase { .... }
...

I’m trying to clean up some legacy code, and there’s one place where a ton of repetitive duplication can all be factored out into a single routine via generics. (I’m thinking generics because when this routine is called, it needs to operate on only one of the concrete classes.)

The routine looks like

public <Thing extends AbstractBase> void someFunc()
{
    another_function_call (Thing.concreteSpecialToken);
    // could also be
    //   another_function_call (Thing.concreteSpecialToken());
    // if methods are more feasible than fields

    // Cannot use
    //   another_function_call (Thing().concreteSpecialToken());
    // because creating instances of these types is a Major Operation[tm]
}

I’m leaving out about a zillion lines, but that’s the important part: someFunc() is type parametric (it actually takes arguments but none of them are Things so no inference). Eventually I need to fetch a special token and this is where I’m getting fuzzy.

The tokens are huge-ass unique strings for each concrete class. They’re class-based, not instance-based. The actual token value is declared as a private static final field in each subclass.

So I need to use the public methods/fields of a base class to (eventually) get to the private static field of a subclass. Obviously I can’t declare an abstract static method in the base, because that makes no sense. If the data were instance-based, then this would be trivial, with a polymorphic getter in the base class, but the subclass stuff is static.

I feel like I’m missing a feature of Java generics here, but I can’t use Thing.whatever() unless the whatever is something that’s possible to declare in the abstract base class. I’m running up against either limitations of Java or my lack of expertise trying to bridge the gap. The one attempt I made that seemed promising also had a ton of code duplication all the way down the class hierarchy, defining abstract methods with the exact same code over and over… which is what generics are supposed to help prevent!

  • 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-15T10:32:33+00:00Added an answer on June 15, 2026 at 10:32 am

    I’m running up against either limitations of Java or my lack of expertise trying to bridge the gap.

    It’s a limitation of Java, although a fairly reasonable one IMO. Basically you’re still trying to use static members as if they were polymorphic, and that’s not going to work – generics don’t help you there.

    Options:

    • Use reflection… but bear in mind that type erasure means you can’t get at the Class for Thing unless you pass it in explicitly
    • If you’ve got an instance of Thing anyway, just make it an abstract instance member, which in each implementation happens to return the value of a static field
    • Create a separate type hierarchy which will use instance members
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This seems like a pretty softball question, but I always have a hard time
This seems like quite a silly basic question but it seems something I have
This seems like a trivial R question, but I didn't find any convincing solution.
This seems like a repeated question but i'm not able to get my answer.
This seems like the simplest Git question, but I can't find ANYTHING on it.
This seems like a must have form input for a mobile ui framework, but
This seems like a noob question, but the simple answer is eluding me. I
This seems like a stupid question, but I'm tripping over it at the moment.
This seems like a simple question, but I haven't been able to find the
This seems like a really simple question but somehow my Google-Fu failed me. What's

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.