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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:40:48+00:00 2026-06-17T12:40:48+00:00

Why is this enum declared in its own file. Is there an advantage to

  • 0

Why is this enum declared in its own file. Is there an advantage to this? Also how would I be able to put this in one of the 2 files. I really have no clue what I am doing. Please also explain this in a simple way, since this is a textbook example, and I am fairly new to Java.

ScaleName.java

enum ScaleName {celsius, fahrenheit, kelvin, rankine};

Temperature.java

class Temperature {

    private double number;
    private ScaleName scale;

    public Temperature() {
        number = 0.0;
        scale = ScaleName.fahrenheit;
    }

    public Temperature(double number) {
        this.number = number;
        scale = ScaleName.fahrenheit;
    }

    public Temperature(ScaleName scale) {
        number = 0.0;
        this.scale = scale;
    }

    public Temperature(double number, ScaleName scale) {
        this.number = number;
        this.scale = scale;
    }

    public void setNumber(double number) {
        this.number = number;
    }

    public double getNumber() {
        return number;
    }

    public void setScale(ScaleName scale) {
        this.scale = scale;
    }

    public ScaleName getScale() {
        return scale;
    }
}

UseTemperature.java

class Temperature {

    private double number;
    private ScaleName scale;

    public Temperature() {
        number = 0.0;
        scale = ScaleName.fahrenheit;
    }

    public Temperature(double number) {
        this.number = number;
        scale = ScaleName.fahrenheit;
    }

    public Temperature(ScaleName scale) {
        number = 0.0;
        this.scale = scale;
    }

    public Temperature(double number, ScaleName scale) {
        this.number = number;
        this.scale = scale;
    }

    public void setNumber(double number) {
        this.number = number;
    }

    public double getNumber() {
        return number;
    }

    public void setScale(ScaleName scale) {
        this.scale = scale;
    }

    public ScaleName getScale() {
        return scale;
    }
}
  • 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-17T12:40:49+00:00Added an answer on June 17, 2026 at 12:40 pm

    Why is an enum declared in a separate file, in Java?

    You don’t have to declare an enum in a separate file. You could do this:

    public class Temperature {
        public enum ScaleName {celsius, fahrenheit, kelvin, rankine};
        
        private double number;
        private ScaleName scale;
    
        public Temperature() {
            number = 0.0;
            scale = ScaleName.fahrenheit;
        }
        ...
    }
    

    The only difference between this and making the enum a top level class is that you now need to qualify the name of the enum when you use it in a different class.

    But what is going on in my example is no different to what happens if the enum was any other static nested class. (A nested enum is implicitly static, so we don’t need a static keyword. See JLS 8.9.)


    Why is this enum declared in its own file.

    Because the code author chose to do it this way1.

    Is there an advantage to this?

    Yes. It means that don’t have to qualify the enum to use it … in some circumstances where you would have to if the enum was nested as above.


    Actually, Java does allow you to put multiple top-level classes into the same source file provided that all but one of the classes is "package private". However, doing that is generally thought to be bad style, and it can be problematic for some tool chains … I have heard.


    1 – If you want to know the real reason why the authors of the textbook chose to do it that way, you would need to ask them!

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

Sidebar

Related Questions

in java an enum can be declared like this enum MyEnum { ONE(descr for
I have this enumeration: enum battleType {localEnemy,multiplayerEnemy}; Which is declared in the .h file
I have declared this enum in my header file: enum wildcard { .... ....
Hi I have an enum declared like this: typedef enum { Top, Bottom, Center
I have an interface declared in its own header MyInterface.h: class MyInterface{ public: virtual
I have seen constructs with an enum declared inside an enum. What is this
I want strong enum types. C++0x has this feature but unfortunately they also require
I often see Simple (non-Flags) Enums declared like this: enum Something { Thingy =
I have declared an enum as follows: enum fileType {typeA, typeB}; This is causing
I have an enum I have declared in some .h file: typedef enum {

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.