in my public class card
, if I do not declare this class as static the program will not run, and all my new card objects will return the following error:
non-static variable this cannot be referenced from a static context
card card1 = new card();
Firstly, my professor has told me that the class should not be static, but has not explained why.
Secondly, I do not understand why the main method (I know it's static) cannot reference a non-static variable.
I think I'm not understanding something fundamental here, but I need to understand what's going on here before moving on.
I've found this definition:
the keyword static indicates that the particular member belongs to a type itself, rather than to an instance of that type.
I have really no clue what that means. A particular member, is that referring to an object? an instance of that type? also sounds like its referring to an object that belongs to a specific class. To me something that declared static is private, and only other classes that are declared static can operate with each other - is that right? If so, why not just use the word 'private'.
The code:
import java.util.Random;
public class playing_cards {
public class card // sets up class
{
int face = 0;
int suit = 0;
String faceText;
String faceValue;
String suitValue;
String suitText;
public card() // card constructor, initially generates card number and suit
{
int face = 0;
int suit = 0;
Random generator = new Random();
face = generator.nextInt(13) + 1;
suit = generator.nextInt(4) + 1;
}
public int getFace() // sets up card face value
{
Random generator = new Random();
face = generator.nextInt(13) + 1;
return face;
}
public int getSuit() // sets up card suit value
{
Random generator = new Random();
suit = generator.nextInt(4) + 1;
return suit;
}
public String getFacetext() // gets card numeric value textually
{
switch(face)
{
case 1:
faceText = "one";
break;
case 2:
faceText = "two";
break;
case 3:
faceText = "three";
break;
case 4:
faceText = "four";
break;
case 5:
faceText = "five";
break;
case 6:
faceText = "six";
break;
case 7:
faceText = "seven";
break;
case 8:
faceText = "eight";
break;
case 9:
faceText = "nine";
break;
case 10:
faceText = "ten";
break;
case 11:
faceText = "eleven";
break;
case 12:
faceText = "twelve";
break;
case 13:
faceText = "thirtee";
break;
}
return faceText;
}
public String getSuittext() // gets card's suit value
{
switch (suit)
{ case 1:
suitText = "hearts";
break;
case 2:
suitText = "spades";
break;
case 3:
suitText = "diamonds";
break;
case 4:
suitText = "clubs";
break;
}
return suitText;
}
public String setCard(int face, int suit) // sets the card to passed through toString
{
switch (face)
{
case 1:
faceValue = "ace";
break;
case 2:
faceValue = "two";
break;
case 3:
faceValue = "three";
break;
case 4:
faceValue = "four";
break;
case 5:
faceValue = "five";
break;
case 6:
faceValue = "six";
break;
case 7:
faceValue = "seven";
break;
case 8:
faceValue = "eight";
break;
case 9:
faceValue = "nine";
break;
case 10:
faceValue = "ten";
break;
case 11:
faceValue = "jack";
break;
case 12:
faceValue = "queen";
break;
case 13:
faceValue = "king";
break;
}
switch (suit)
{
case 1:
suitValue = "hearts";
break;
case 2:
suitValue = "spades";
break;
case 3:
suitValue = "diamonds";
break;
case 4:
suitValue = "clubs";
break;
}
return suitValue;
}
public String toString()
{
String result = faceValue + " of " + suitValue;
return result;
}
}
public static void main(String[] args) {
card card1 = new card();
card card2 = new card();
card card3 = new card();
card card4 = new card();
card card5 = new card();
card card6 = new card();
card card7 = new card();
card card8 = new card();
card card9 = new card();
card card0= new card();
card1.setCard(card1.getFace(), card1.getSuit()); // sets each cards numeric value and suit and passes through toString
System.out.println(card1.toString());
card2.setCard(card2.getFace(), card2.getSuit());
System.out.println(card2.toString());
card3.setCard(card3.getFace(), card3.getSuit());
System.out.println(card3.toString());
card4.setCard(card4.getFace(), card4.getSuit());
System.out.println(card4.toString());
card5.setCard(card5.getFace(), card5.getSuit());
System.out.println(card5.toString());
System.out.println("");
card6.setCard(1, 1);
System.out.println(card6.toString());
card7.setCard(13,4);
System.out.println(card7.toString());
card8.setCard(15,5);
System.out.println(card8.toString());
card9.setCard(2,2);
System.out.println(card9.toString());
card0.setCard(3,2);
System.out.println(card0.toString());
System.out.println("");
System.out.println("Card one's suit is " + card1.getSuittext());
System.out.println("Card one's textual face value is " + card1.getFacetext());
}
}