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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:57:43+00:00 2026-05-27T18:57:43+00:00

Possible Duplicate: Why can't enum constructors be protected or public in Java? The following

  • 0

Possible Duplicate:
Why can't enum constructors be protected or public in Java?

The following code just demonstrates the use of enum in Java. It has nothing to do with and just sums up the values of the members of that enum.

package enumtest;

enum Sum
{        
    Pen(10),Pencil(15),Eraser(5);

    private int value;

    private Sum(int value)
    {
        this.value=value;
    }

    public int getValue()
    {
        return(value);
    }
}

final public class Main
{
    public static void main(String...args)
    {
        int sum=0;

        for(Sum o:Sum.values())
        {
            sum+=o.getValue();
        }

        System.out.println("sum = "+sum);
    }
}

It displays sum = 30 on the console. Just one question here. Generally, in a class, constructors are declared public then why can constructors in enum only be declared private? The other modifiers public and protected are not allowed with it. Even declaring it public causes a compile-time error. Why?

  • 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-27T18:57:44+00:00Added an answer on May 27, 2026 at 6:57 pm

    You can also declare them without any keyword, e.g. just Sum(int value).

    The reason is because Java itself will create the various instances for you, and as Singletons – which is necessary for Enums to work correctly. public or protected would imply that or allow additional instances of the same enum to be created by other callers, which would break the Singleton guarantee.

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

Sidebar

Related Questions

Possible Duplicate: Can I use predefined namespaces when loading an XDocument? I have following
Possible Duplicate: How can I use a carriage return in a HTML tooltip? I'd
Possible Duplicate: How can I convert my java program to an .exe file ?
Possible Duplicate: Can you write object oriented code in C? I'm wondering if it's
Possible Duplicate: Can I use ASP.NET MVC together with regular ASP.NET Web forms Let's
Possible Duplicate: Can I just make up attributes on my HTML tags? Hi, I
Possible Duplicate: Can you write object oriented code in C? Hi, can someone point
Possible Duplicate: Can I use a generated variable name in PHP? Am stuck here!
Possible Duplicate: Can someone Explain this jQuery code? I have posted this before, but
Possible Duplicate: Enum type constraints in C# Is it possible to use enum types

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.