Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6988489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:05:17+00:00 2026-05-27T19:05:17+00:00

Scenario: I want to have an enum containing all the playing cards in a

  • 0

Scenario:
I want to have an enum containing all the playing cards in a standard deck. For this example ignore the jokers.

Writing

enum Cards {
    SPADE_1(0, 1),
    SPADE_2(0, 2),
    etc.

feels wrong.

I’d like to be able to do something like this

enum Card {
    for (int suit=0; suit<4; suit++) {
        for (int face=1; face<13; face++) {
            new Card(suit, face);
        }
    }
}

I’ve considered defining card as a class containing suit and face fields, where suit and face are themselves enums. However in other scenarios (such as jokers having the suits red and black) this would allow for invalid card objects to be created (ie a joker of diamonds, or a red 10).

Self-answer:
Apparently I don’t have enough rep to post an answer to my own question.

I'm not sure if it's considered good form to answer my own question, but @Paul just gave me a brainwave.

Declare Card to have a private constructor, and use a
    static Card getCard(suit, face)
method to validate combinations before returning them.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:05:18+00:00Added an answer on May 27, 2026 at 7:05 pm

    I don’t think it can be done using enum but we can implement class as enum. you can do something like below.

    Implementations:

    public class Card {
        private int suit;
        private int face;
    
        private Card(int suit, int face){
            this.suit = suit;
            this.face = face;
        }
    
        public int getSuit(){
            return this.suit;
        }
        public int getFace(){
            return this.face;
        }
        public static Card[] cards = new Card[52];
    
          static{
            int counter =0;
            for (int i=0; i<4; i++) {
                for (int j=0; j<13; j++) {
                   cards[counter] = new Card(i, j);
                   counter++;
                }
            }
          }
    }
    

    EDIT:
    set the counter of the card. Earlier it would throw NullPointerException for index more than 15.

    USAGES:

    System.out.println("face of the card:"+Card.cards[10].getFace());
    System.out.println("suit of the card:"+Card.cards[10].getSuit());
    

    OUTPUT:

    face of the card:7
    suit of the card:3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this tricky scenario and want so advice to you. My client have
The Scenario: We have multiple nodes distributed geographically on which we want to have
The customer want to have the following scenario: Customer hands out link (webapp address)
I have a scenario where I want to prove a lemma including a number
I have a simple scenario where i want to output only json to the
I have the following scenario where I want to pass in string and a
I have a scenario where I want to reduce the margin of a Button
I have a scenario where i want to match specific word and then match
I have a scenario where I want to be able to SELECT rows from
I have a scenario where I want to toggle series based on checkbox selection.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.