-3

I have this abstract class :

public abstract class Pile {

    protected Deck d=new Deck(); 

    public Pile(){

    }
    public abstract void PrintCard();
    public abstract boolean IsEmpty();
}

I want the d field and its value to be the same for all objects of type Pile (and its subclasses).

I want this to happen because I remove cards and add them do the deck so it has to be only one deck.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • 4
    ...make it `static`? Or inject the `Deck` as a parameter of `Pile`. – Andy Turner Nov 18 '15 at 16:17
  • if i make it static it wont create another deck on new object? i cant add object i have to many different subclasses to give it like this thanx – Γιώργος Φιωτάκης Nov 18 '15 at 16:19
  • 1
    No, it won't. However, each instance of `Pile` will be using the same `Deck`, which may not necessarily be a good thing. Think about it; you have a room full of different groups of people playing a multitude of card games. Does each group have its own `Deck`, or do all of the groups draw from a single `Deck` placed on a table in the middle of the room? – JonK Nov 18 '15 at 16:20
  • @ΓιώργοςΦιωτάκης what do you think `static` does? Maybe read up about it. – Andy Turner Nov 18 '15 at 16:25
  • Static means that d will always be that deck. You cannot change the value of d once you set it. – Rabbit Guy Nov 18 '15 at 16:25
  • 1
    @blahfunk "You cannot change the value of d once you set it" that's `final`. – Andy Turner Nov 18 '15 at 16:26
  • @Andy final means that you cannot redefine this in children. – Rabbit Guy Nov 18 '15 at 16:28
  • @blahfunk `final` prevents you creating a static variable of the same name in a subclass *and* it prevents you from changing the value of the variable. However, nothing stops a subclass changing the value of a protected, static, *non-final* field. – Andy Turner Nov 18 '15 at 16:29
  • @Andy I need to go back to school. Yes, static just means that it is a non object. It is shared by all decedents and only "initialized" at the start of execution and belongs to the class, not to any specific object. I apologize for the confusion I may have caused here. – Rabbit Guy Nov 18 '15 at 16:33

1 Answers1

2

The easiest way is simply to make d static (it's also worth making it final, so that no subclass can replace it with a different instance):

public abstract class Pile {
  protected static final Deck d=new Deck(); 

  public abstract void PrintCard();
  public abstract boolean IsEmpty();
}

However, static variables like this can be problematic, especially from a testing perspective. It is also tricky to change later if you need some Piles to have one Deck whilst others have a different Deck (e.g. JonK's example).

You can alternatively inject the Deck as a parameter:

public abstract class Pile {
  protected final Deck d;

  public Pile(Deck d) {
    this.d = d;
  }

  public abstract void PrintCard();
  public abstract boolean IsEmpty();
}

You can now inject a value of your choosing (e.g. a mock) when you construct the Pile. The second solution is the one I would choose.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • 3
    Second is a much better solution. – Sotirios Delimanolis Nov 18 '15 at 16:24
  • The second solution here not only is easier to work with but it also allows to KISS (keep it simple, stupid) – Rabbit Guy Nov 18 '15 at 16:27
  • i need to change the deck so i cant make it final , i thought your second solution to but i have 4 different piles with 3 objects needed each one of them so it is not so easy to insert a deck like this, basically one sec can my deck class contains an array of different card and i have some methods to delete and add cards if i make deck final will i be able to call them? – Γιώργος Φιωτάκης Nov 18 '15 at 16:31
  • Making the variable `final` means that you can't reassign `d`, i.e. `Pile.d = new Deck()` will be a compiler error; you can still call methods on `d` which might change it, e.g. `d.addCard()`. If you need multiple `Pile`s to share a single deck, you should keep it final, so that no `Pile` can arbitrarily change its `Deck`. – Andy Turner Nov 18 '15 at 16:34
  • my point is final will protect only the value or and its contains? – Γιώργος Φιωτάκης Nov 18 '15 at 16:34