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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:26:23+00:00 2026-05-30T13:26:23+00:00

Consider the example: enum SomeEnum { VALUE1(value1), VALUE2(value2), VALUE3(value3) ; private String value; private

  • 0

Consider the example:

enum SomeEnum {
    VALUE1("value1"),
    VALUE2("value2"),
    VALUE3("value3")
    ;
    private String value;

    private SomeEnum(final String value) {
        this.value = value;
    }

    //toString
    public String toString() {
        return value;
    }
}

How come can we do this (and the value really changes)?

SomeEnum.VALUE1.value = "Value4";
System.out.println(SomeEnum.VALUE1);

Isn’t that enum instance(s) are implicitly static and final? Also, since value is private, why can I access it outside other classes?

  • 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-30T13:26:24+00:00Added an answer on May 30, 2026 at 1:26 pm

    No-one seems to have addressed the private aspect. My guess is that you’re accessing the private field from a containing type – that your enum is actually a nested type, like this:

    class Test
    {
        static void Main() {
            // Entirely valid
            SomeEnum.VALUE1.value = "x";
        }
    
        enum SomeEnum {
            VALUE1("value1");
    
            private String value;
    
            private SomeEnum(final String value) {
                this.value = value;
            }
        }
    }
    

    That’s entirely legitimate and normal – you can always access private members of a nested type from the containing type.

    If you make the enum a top-level type, you won’t see this.

    As for changing values – as everyone else has said, VALUE1 is implicitly static and final, but that doesn’t stop you from changing VALUE1.value. Again, this is entirely in accordance with how Java works elsewhere – if you have a static field of type List, you can still add entries to it, because that’s not modifying the field itself.

    If you want to make SomeEnum properly immutable, make the value field final.

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

Sidebar

Related Questions

Please consider this example class: [Serializable] public class SomeClass { private DateTime _SomeDateTime; public
Consider this example: public class Factory { private List<ISubFactory> subFactories; public Factory(List<ISubFactory> subFactories) {
Consider this example : import java.lang.reflect.Field; public class Test { public static void main(String[]
Consider this example. public class myclass { public void... public int ... private class
Let's consider this example : public class Shared { private int attribute; public Shared()
Consider following example : public class SomeBusinessLayerService : DataService<MyEntityContainer> { [WebInvoke] void DoSomething(string someParam)
Consider this example: #include <iostream> class myclass { public: void print() { std::cout <<
Consider this contrived example: # Dispatch on value of fruit_kind: TYPE_A = :apple TYPE_B
I have a function accepting an enum value as parameter. As an example, consider
Consider this example: #include <iostream> #include <string> #include <vector> #include <iterator> int main() {

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.