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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:22:17+00:00 2026-05-15T22:22:17+00:00

I defined an enum type that implements an interface as follows: public enum MyEnum

  • 0

I defined an enum type that implements an interface as follows:

public enum MyEnum implements MyInterface
{
    val1, val2, val3;
    private MyEnum() {}
    private MyEnum(Parcel in)
    {
        readFromParcel(in);
    }

    public void readFromParcel(Parcel in)
    {
        MyEnum val = MyEnum.values()[in.readInt()];
        // ??? How to I assign val to my current enum?
    }

}

How do I access the value of the current enum object so I can make the assignment inside of readFromParcel() ? (Please see comment in code)

  • 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-15T22:22:18+00:00Added an answer on May 15, 2026 at 10:22 pm

    Inside an instance method, you can refer to the “current enum object” as simply this. It works this way because enum constants are actual objects, i.e. instances of a class — a very special type of class, but a class nonetheless. (Note that I mentioned that these are enum constants: it is the convention in Java to use all uppercase letters when naming constants.)

    Your usage of values() is also very peculiar (not to mention that it’ll perform horribly since a new array must be constructed at each call). Perhaps you’d want to take a look at EnumMap, which is a special kind of Map optimized for enum constants as keys.

    If you’re trying to mutate fields contained in these enum constants, then you should seriously consider a redesign. You should generally minimize mutability anyway, but having these static singletons be mutable does not sound like a good design. Instead of having these mutable fields intrinsic within the enum constants themselves, a Map from the constants to these mutable values would be a much better design.

    See also

    • Java Tutorials/enum
    • Effective Java 2nd Edition
      • Item 15: Minimize mutability
      • Item 31: Use instance fields instead of ordinals
      • Item 32: Use EnumSet instead of bit fields
      • Item 33: Use EnumMap instead of ordinal indexing

    Various questions on Java enum

    • Configurable Values in Enum
    • How to efficiently use Enum objects as keys in the Map data structure?
    • Is there a a C-like way to get item number from enum in java ? (yes, but why would you???)
    • Get enum by its inner field
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 510k
  • Answers 510k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Sounds like it is a case of permissions, the IIS… May 16, 2026 at 5:05 pm
  • Editorial Team
    Editorial Team added an answer Kudos on learning by doing. Here are suggestions of "opportunities"… May 16, 2026 at 5:05 pm
  • Editorial Team
    Editorial Team added an answer You have to copy the string into the array: strcpy(buf.ProjectName,… May 16, 2026 at 5:05 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

How come java doesn't allow the following generic return type: public <T extends Enum<T>
I have a class that should read a message, extract its type from the
I have several classes that implement the same interface. interface IBidManager<E> where E :
I have some client data that I am reading in, and I've defined an
Given a class hierarchy where the base class defines a recursive self-type: abstract class
A UI question: is there some consensus on the best (defined as the one
Hi currently have the problem, that I want to insert values in a postgres
I'm implementing enumeration using a base class that defines a variety of methods. The
class Program { static void Main(string[] args) { string value = "12345"; Type enumType
I have a fairly simple expression parser that uses the Linq.Expression namespace. Input is

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.