1

i'm using Piles. Piles in my case, are objects containing Card ArrayLists. I'm going to be using more than 1 of them, each different to their purpose.


What i'm trying to do here is make 2 methods:

getCards: that moves Object refernces from one Pile to another

removeCards: that deletes a number of arraylist entries

import java.util.ArrayList;

public abstract class Pile {

public static ArrayList <Card> newPile;

public Pile() {
    newPile = new ArrayList <Card>();
}


public abstract void displayPile(); 

public void removeCards(int numOfCards) {
    for (int i = 0; i < numOfCards; i++) {
        Pile.newPile.remove(0);
    }
}

public static void getCards(int numOfCards, Pile fromPile) {
    for (int i = 0; i < numOfCards; i++) {
        Pile.newPile.add(i, fromPile.newPile.get(i));             // here is the error
    }
}

}

The error is:

The static field Pile.newPile should be accessed in a static way.


Anyone cares to explain what i'm doing wrong and how can i deal with it?

nic3ts
  • 347
  • 1
  • 2
  • 13
  • `newPile` is `static`. This means that there is only one `ArrayList `, not an `ArrayList` for each `Pile`. – Paul Boddington Dec 01 '15 at 03:39
  • @PaulBoddington removing `static` from `newPile` gives me "cannot make a static reference to the non-static field Pile.newPile" and doesn't remove the initial problem. – nic3ts Dec 01 '15 at 03:44
  • Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask) before attempting to ask more questions. –  May 24 '17 at 04:36

2 Answers2

1

Static member variables are shared by all instances of the class to which they belong. Because newPhile is static, you either need not to edit it in each individual object:

enter image description here (http://itcareerworld.com/wp-content/uploads/2015/02/static-keyword-diagram-1.png)

So because it is static, there is only one copy of it. Either remove the static from the variable and the method or don't make an object of it to instantiate, because it is already shared across the objects. I suggest the first.

I noticed your comment when you said:

cannot make a static reference to the non-static field Pile.newPile

Just make everything non static (THE VARIABLE AND THE METHOD) if you are doing that option.

Ruchir Baronia
  • 7,406
  • 5
  • 48
  • 83
  • mate, i removed it, both from `newPile` and the method, but the "cannot make a static reference ..." is now in both methods, in the beginning of the sentece in the for loops (Pile.newPile.....) – nic3ts Dec 01 '15 at 04:12
  • A static method belongs to the class, a non-static method belongs to an instance of the class. Are you still using the object to call the method now, because since it is not static, you need to use an object now. – Ruchir Baronia Dec 01 '15 at 04:16
  • @NickTritsis Now did you get it? If my answer helped a little bit then please mark my answer best answer. If you have any more questions then don't be afraid to ask I'm happy to help – Ruchir Baronia Dec 01 '15 at 05:37
  • Thanks mate, i i got it. I'm really glad you explained the static modifier cause i didn't quite give it the proper attention in the first place, but it's pretty clear to me now :) You'll get the best answer and upvote for thatm don't worry, i know how the stackoverflow works :) Also, upvote my question too if you don't mind, i'd hate to get a negative question score for such a minor issue :-/ Again, thanks for the help ^^ edit: not enough rep for an upvote, but it's marked as best answer ^^ – nic3ts Dec 01 '15 at 17:34
  • @NickTritsis I upvoted your question...I know how you feel. Currently, I am at risk for getting banned from asking questions because of the excessive downvotes I received on [this](http://stackoverflow.com/questions/33888178/do-something-when-app-starts) and [this](http://stackoverflow.com/questions/33931352/how-can-i-remove-button), and the only way to stop the message from showing is to get more likes on those questions...Anyway, I have liked your question, and am glad to help you. Just ask me for any more help! – Ruchir Baronia Dec 03 '15 at 01:54
  • we all get bad days bro ... Your help with the visibility modifier was precious for me, I upvoted your questions :-) Good luck and ty! – nic3ts Dec 04 '15 at 02:59
0

Static variables can be accessed in static method without using reference variable. You can try it without using reference variable.

Sachin Sridhar
  • 434
  • 4
  • 13