0

Before marking duplicate please read:

I did read the following article and found the potential 'solutions' not to be what I was looking for as most of them address using an equals() method.

Classes sharing the same super class instance

What I am looking to do is to have multiple static accessible subclass instances that can share a single parent class instance so that changes to the parent instance will be reflected. I am open to a variety of solutions however I am trying to minimize the number of object references and must store and call these instances in a static manner.

Hopefully the following code will explain in more detail what I am trying to achieve.

// ParentClass

public class School {
    private static final HashMap<String,School> pmapping = new HashMap<>();

    private final String pname;
    private int pvalue;

    private School( String name, int value) {
        pname = name;
        pvalue = value;

        pmapping.put(name,this);
    }

    public static School build( String name, int value) {
        return this( name, value);

    public static School instance( String name) {
        // Yes there will be exception handling in the final version
        return mapping.get(name);

    public String name() {
        return pname;
    }

    public void setValue( int value) {
        pvalue = value;
    }

    public void getValue( ) {
        return pvalue;
    }
}

// ChildClass

public class Subschool extends School {
    private static final HashMap<String,Subschool> cmapping = new HashMap<>();

    private final String cname;

    private Subschool( School school, String name) {
        // what I would like to happen
        super = school;
        cname = name;

        cmapping.put( cname, this);
    }

    public static School build( String school, String name) {
        School s = School.instance(school);
        return this( s, name);

    public static School instance( String name) {
        // Yes there will be exception handling in the final version
        return cmapping.get(name);

    public String name() {
        return cname;
    }
}

// TestClass

public class Test {
    public static main(String[] args){
        // What I need to occur
        School illusion = School.build( "Illusion", 1);
        Subschool figment = Subschool.build( "Illusion", "Figment");
        Subschool shadow  = Subschool.build( "Illusion", "Shadow");

        illusion.setValue( 3);
        if( figment.getValue() == 3) 
            System.out.println( "Check 1 Succeed");
        if( figment.getValue() == shadow.getValue() )
            System.out.println( "Check 2 Succeed");
    }
}
Community
  • 1
  • 1
  • 5
    Your question sounds like it may in fact be an [XY Problem](http://mywiki.wooledge.org/XyProblem) where you ask "how do I fix this code problem" when the best solution is to use a different approach entirely. Consider telling us the overall problem that you're trying to solve rather than how you're currently trying to solve it. – Hovercraft Full Of Eels Apr 05 '16 at 17:38
  • Do you really want to use an abstract factory design pattern? – Hovercraft Full Of Eels Apr 05 '16 at 17:40
  • 1
    Sounds like you may well want composition instead of inheritance. – Jon Skeet Apr 05 '16 at 17:41
  • 1
    `static` and inheritance are sort of orthogonal, in Java at least. – Boris the Spider Apr 05 '16 at 17:42
  • 1
    Moreover, "_I am trying to minimize the number of object references and must store and call these instances in a static manner_" - I don't follow. First, why can another `class` - say a `Factory` - not store these instances in a **singleton** manner rather than a `static` one? Secondly, how does `static` and "_instance creation_" even relate. I can create a billion instances of a `class` with `static` fields or one instance of a class with instance fields... – Boris the Spider Apr 05 '16 at 17:46
  • @HovercraftFullOfEels This is only a small psuedo code that I wrote up to ask this question. I have been working on a program to allow players to create "Spells" for d20 roleplaying games. I settled on the Abstract Factory Design pattern after about 2 days of research. If you think there is a better solution I am not opposed. – Peter Wallace Apr 05 '16 at 17:54
  • 1
    @PeterWallace: if Jon Skeet says use composition, you'd better use composition and not inheritance to solve this problem. – Hovercraft Full Of Eels Apr 05 '16 at 17:56
  • @JonSkeet I am not opposed to composition but I have another class Spell that will have a School field and I would like to have it store either a School instance or a Subschool instance as Subschool information is not required. I am not opposed to a AbstractSchool solution and using composition but am a little fried right now and haven't found a solution I like. – Peter Wallace Apr 05 '16 at 18:00
  • 2
    @PeterWallace: I'm afraid that's a very woolly description. Fundamentally, it's not like a subclass instance has an associated superclass instance - there's *one object*. So the idea of multiple subclass instances being associated with the same superclass instance just doesn't work, conceptually... whereas composition certainly *would* let each `Subschool` have a reference to a `School`, and multiple `Subschool` instances could refer to the same `School`. Also note that there's no such thing as a "static instance". – Jon Skeet Apr 05 '16 at 18:01
  • @PeterWallace, refer to : http://stackoverflow.com/questions/17164375/subclassing-a-java-builder-class and http://stackoverflow.com/questions/24243240/extending-classes-using-builder-pattern – Ravindra babu Apr 05 '16 at 18:05
  • @BoristheSpider I have multiple modules that need to get a the same instance of a class. Singletons last time I read were classes with a single instance. My classes can have multiple instances but each instance must be unique. I could create a separate Factory class for these and pass that factory class to each module but I decided it was easier to use the abstract factory design integrated into the class and have each module reference that as there will never be more then one factory for each class. Refactoring may require me to separate the factory out but will not address my question – Peter Wallace Apr 05 '16 at 18:14
  • @JonSkeet Sorry for my poor vocabulary use, I know that there is no such thing as a static instance. I meant an instance that can be referenced using a static method utilizing designs like Singletons or Factories. Could you clarify what you mean by woolly? – Peter Wallace Apr 05 '16 at 18:19
  • @JonSkeet Thanks for your timely responses today. I am going to leave the question open but I am going to try and see if I can make a solution I like using AbstractSchool as a common inherited class for both School and Subschool while uses composition for the relationship between Subschool and School. – Peter Wallace Apr 05 '16 at 18:38

0 Answers0