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 8271225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:47:14+00:00 2026-06-08T06:47:14+00:00

I am finding it tuff with enums. This is an example from Kathy Siera

  • 0

I am finding it tuff with enums. This is an example from Kathy Siera book :

    public class WeatherTest {

    static Weather w;

    public static void main(String[] args) {
        System.out.print(w.RAINY.count + " " + w.Sunny.count + " ");
    }
}

enum Weather {

    RAINY, Sunny;
    int count = 0;

    Weather() {
        System.out.print("c ");
        count++;
    }
}

The output is c c 1 1. Understood.
Now I thought what if the count field was static? Would the output be c c 2 2 ?
Based on it, I modified the count variable to static.
But then what I see is this :
Compile Time Error : Illegal reference to static field from initializer.

Searching on net I found this was some kind of a loop-hole by Sun and it allows static methods that can change static fields. Ok.. So now I use a static method incr to do my job :

     class WeatherTest {

    static Weather w;

    public static void main(String[] args) {
        System.out.print(w.RAINY.count + " " + w.Sunny.count + " ");
    }
}

enum Weather {

    RAINY, Sunny;

    Weather() {
        System.out.print("c ");
        incr();
    }
    static int count = 0;

    static void incr() {
        count++;
    }
}

To my surprise, I get the output : c c 0 0 !
Can anyone please explain this behaviour to me before I shoot myself?

  • 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-08T06:47:15+00:00Added an answer on June 8, 2026 at 6:47 am

    Enum values can be thought of as glorified static fields ( they are under the covers ).

    So, if your Weather were a regular Java class, it would have been something like this:

     class Weather {
       public final static Weather RAINY = new Weather( );
       public final static Weather Sunny = new Weather( );
    
       static int count = 0;
    
       Weather( ) {
         System.out.print("c ");
         incr();
       }
    
       static void incr()
       {
          count++;
       }
     }
    

    You can see that count is declared AFTER both enum values, that means it is also initialized after both values are created. Also, each function call that encounters uninitialized static variable treats it as initialized with default value ( for int it’s 0).

    Because you never call incr after count has been properly initialized, you see its value as still 0

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

Sidebar

Related Questions

Finding myself in the situation where I have a method with this signature void
Finding lots of mixed answers on this from Google - but mostly 2-3 years
I am having trouble finding an answer to this. Consider the clipping code below:
I'm having trouble finding a way to do this in a way that doesn't
Finding the answer to this is turning out to be much more difficult than
After finding the difflib.SequenceMatcher class in Python's standard library to be unsuitable for my
After finding the difflib.SequenceMatcher class in Python's standard library to be unsuitable for my
I am finding the Zend_Controller_Request_Abstract class quite insufficient for my application but maybe I
I'm finding in this situation: I've a legacy service (written in C#) that just
After finding the fastest string replace algorithm in this thread , I've been trying

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.