So static variables will update across all instances of the specified class. On the other hand, static methods allow you to access a function within a class without creating a instance, therefore you cannot access any non-static fields because they would only exist if the a new instance is created. Take a look at the following example at a simple class I created below to explain the concept:
public class Employee
{
private static int idcount = 1;
private int id;
public Employee()
{
this.id = idcount;
idcount++; //cant use "this" with static variables
}
public int getId()
{
return this.id;
}
public static int getIdCount()
{
return idcount; //can only use static variables
}
public static void main (String [] args)
{
Employee employee1 = new Employee();
Employee employee2 = new Employee();
System.out.println(employee1.getId()); //will print 1
System.out.println(employee2.getId()); // will print 2
System.out.println(Employee.getIdCount()); // will print 3 and see how the static method is called straight on the Employee class and not a instance
}
}
So if you run the java class above you will see that static variable will get updated across both instances of the Employee class.
So for your problem what you could do is have a card class and within you have a static variable that holds the Card instances that are created and a static method that gets a specific card for example
import java.util.ArrayList;
public class Card
{
private static ArrayList<Card> Cards = new ArrayList<Card>();
private int index; //field to store the index of the card in the array
private String name; //test field
public Card(String name)
{
this.name = name;
this.index = Cards.size();
//when you create a new card add it to the list
Cards.add(this);
}
public int getIndex()
{
return this.index;
}
public String getName()
{
return this.name;
}
public static Card getCard(int index)
{
return Cards.get(index);
}
public static void main(String [] args)
{
Card KingOfClubs = new Card("King Of Clubs");
Card KingOfHearts = new Card("King Of Hearts");
System.out.println(Card.getCard(KingOfClubs.getIndex()).getName()); // will print King of Clubs
System.out.println(Card.getCard(KingOfHearts.getIndex()).getName()); // will print King of Hearts
}
}
I have no clue on what your actually doing so i just wanted to explain to you how static variables and method works. Hope this helped you.