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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:26:37+00:00 2026-05-23T15:26:37+00:00

I’ve read that you cannot declare static variables/methods inside a generic class and I

  • 0

I’ve read that you cannot declare static variables/methods inside a generic class and I really have no idea how to solve my problem or work around it so I ask for your guidance.

What I want is a generic “index” that all of my core classes will extend.
I’m creating a game-engine and an example is that I will have different gamestates who all extends State who in turn extends Nexus<State>. The reason I want the static head and tail is so that I can keep a linked list of all gamestates since they’re all added to that list upon creation.

Another example is that I will have different gameobjects who all extends GameObject who in turn extends Nexus<GameObject>.

This is the index called Nexus:

public abstract class Nexus<T> 
{

    private static T head = null;
    private static T tail = null;

    private T next = null;
    private static int num = 0;

    protected Nexus() { this.Add( (T)this ); }

    public T Add( T obj )
    {

        ((Nexus)obj).next = null;
        if( num++ == 0 ) head = tail = obj;
        else             tail = ( tail.next = obj );

        return obj;

    }

}

If anyone got another solution or a workaround I’m all ears!

  • 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-23T15:26:37+00:00Added an answer on May 23, 2026 at 3:26 pm

    Try this approach: Define a protected abstract method that subclasses implement to return a static object for their class.

    There may be some logic issues etc, but the basics of the answer are here (ie this compiles):

    EDITED: Now delegating to HeadAndTail

    /** <T> A subclass of Nexus */
    abstract class Nexus<T extends Nexus<T>> { // This syntax lets you confine T to a subclass of Nexus
        private T next;
    
        protected Nexus() {
            this.add((T) this);
        }
    
        public T add(T obj) {
            // Delegate to HeadAndTail
            return getHeadAndTail().add(obj);
        }
    
        /** @return a static for the class */
        protected abstract HeadAndTail<T> getHeadAndTail();
    }
    
    /** Bundled into one Object for simplicity of API */
    class HeadAndTail<T extends Nexus<T>> {
        T head = null;
        T tail = null;
        int num = 0;
    
        public T add(T obj) {
            obj.next = null;
            if (num++ == 0)
                head = tail = obj;
            else
                tail = tail.next = obj;
    
            return obj;
        }
    }
    
    class ConcreteNexus extends Nexus<ConcreteNexus> {
        // This is the static object all instances will return from the method
        private static HeadAndTail<ConcreteNexus> headAndTail = new HeadAndTail<ConcreteNexus>();
    
        protected HeadAndTail<ConcreteNexus> getHeadAndTail() {
            return headAndTail; // return the static
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put

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.