123 123 - 1 year ago 85
Java Question

How to create instance of Card class in Java

I'm new to Java and I'm kind of lost on how classes work syntactically. I have the following class:

import java.util.ArrayList;
import java.util.List;

public class Card {

public final Face face;
public final Suit suit;
private boolean visible;

public enum Face {
TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(
9), TEN(10), JACK(10), QUEEN(10), KING(10), ACE(11);

private int value;

Face(int value) {
this.value = value;

public int getValue() {
return this.value;

public enum Suit {

public Card(Face face, Suit suit){
this.face = face;
this.suit = suit;


public boolean equals(Object o){
if(!(o instanceof Card)) return false;
if(this == o) return true;

return this.toString().equals(o.toString());

public String toString(){
return this.face.name() + this.suit.name();


private static final List<Card> protoDeck = new ArrayList<Card>();
static {
for (Suit suit : Suit.values())
for (Face face : Face.values())
protoDeck.add(new Card(face, suit));

public static ArrayList<Card> newDeck() {
return new ArrayList<Card>(protoDeck); // Return copy of prototype deck

public boolean isVisible() {
return this.visible;

public void setVisible(boolean b){
this.visible = b;


How can I create an instance of this class? For example how would I create a Card that is the 5 of hearts?

Answer Source

Like this:

 new Card(Card.Face.FIVE, Card.Suit.HEART)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download