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

  • Home
  • SEARCH
  • 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 8601665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:53:48+00:00 2026-06-12T01:53:48+00:00

I was reading about enumeration examples in Java from this page. In the first

  • 0

I was reading about enumeration examples in Java from this page.

In the first example, the only part I did not understand was the use of the static keyword in this part of the code:

private static final List<Card> protoDeck = new ArrayList<Card>();

// Initialize prototype deck
static {
    for (Suit suit : Suit.values())
        for (Rank rank : Rank.values())
            protoDeck.add(new Card(rank, suit));
}

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

Why is the protoDeck declared static and final? Following that, a static loop is used to initialize the protoDeck. I know about static variables that they retain their values between instances of objects. But this is a singleton class (private constructor), so it cannot be instantiated.

So what is the benefit of doing it as above? what would be the implications if proteDeck is not static and final ?

  • 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-06-12T01:53:49+00:00Added an answer on June 12, 2026 at 1:53 am

    Technically, this is not a singleton class. (A singleton has one instance, this one has none!)

    It is a factory method for an ArrayList<Card>, and the method newDeck works the same way as a constructor would for a class “CardArrayList”. It’s not a Java constructor but a factory method, but other than that it serves the same purpose: creating new card decks.

    Using a class CardArrayList and subtyping ArrayList would obviously be an alternative. Try this as an exercise: write such as class so that it serves the same purpose. Try to use a constant (static final) for keeping the initial set of objects. You’ll notice that there is little difference between these two, except that this approach clearly says that there is no added functionality, but the result is “nothing but an ArrayList<Card> containing a full deck”. Subclassing might imply that there is extra functionality, such as ensuring the deck is not messed with.

    The static final variable contains the prototype that serves as template for new objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading about clone from Effective Java It says that in clone the
while reading about declare construct from php manual i tried the following example using
Before reading about my problem, first look at this GUI Diagram . There are
I am reading about bash internal variables and came across this IFS example: output_args_one_per_line()
Reading about Kohana templates and saw something I've never seen before: $this->template->title = __('Welcome
Reading about Django, I saw this: http://docs.djangoproject.com/en/1.1/ref/contrib/admin/#ref-contrib-admin - the fancy simple to use admin
I am reading about what an interface contains and I understand in addition to
After reading about the defer attribute at mdn This Boolean attribute is set to
When reading about JQuery best practices, I read this recently: Never include Javascript events
Reading about the problem of creating a read only primitive vector in C# (basically,

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.