I’m working on a personal project. It will be a card game that you might for instance compare to pokemon. unfortunately, I’m running into an error and I can’t figure out what causes it. I’d appreciate any help!
okay, so I’ve got the card class (I’ve left out unnessesary attributes) with a constructor
public class Card
{
String name;
String cardID;
int strFire;
int strEarth;
public Card(String n, String id, int fire, int earth)
{
name = n;
cardID = id;
strFire = fire;
strEarth = earth;
}
}
then I’ve got the Deck class, which should create the instances of all cards.
public class Deck
{
static void createDeck()
{
Card hoax06 = new Card("Nirwadas the Traveler", "hoax06", 3, 2);
System.out.println(hoax06.name); // this works
}
}
and finally, I’ve got the Game class which holds main.
public class Game
{
public static void main(String[] args)
{
Deck.createDeck();
System.out.println(hoax06.name); // hoax06 cannot be resolved to a variable
}
}
I know the answer is probably quite simple but java’s accessing system still confuses me. I’ve also browsed the forum for similar errors but have been unable to apply them to my case. how should I refer to a card from within main?
The card instance is created in the Deck class, so the Game class has no direct knowledge of it. Also, as soon as the createDeck() method ends you lose your reference to the card and the instance is gone.
Here’s a simple example: