user1943931
user1943931

Reputation:

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

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;
    }
}

Upvotes: 28

Views: 60504

Answers (4)

Trevor Arnold
Trevor Arnold

Reputation: 196

I would put an enum in its own file in case I needed to use that enum in multiple different locations throughout my project. You could define the enum for each file you wanted to use it in, but then if you needed to update the enum, you would have to update it in each file. If you define in once and need to update it, you only need to update it the one time. This is not only easier but also ensures you don't miss one of the enum definitions throughout your codebase.

Upvotes: 0

Stephen C
Stephen C

Reputation: 718758

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!

Upvotes: 36

Jack
Jack

Reputation: 133567

An enum is a class and follows same regulations.

Having it on its own file is exactly like moving an inner class in a separate file, nothing more nor less. So yes you can move it inside one of the class and be able to access it from outside with OuterClass.ScaleName syntax.

Upvotes: 5

fge
fge

Reputation: 121712

Enums are not just bare values in Java, they are much more than that. They are classes in their own right (they inherit Object). And consider these:

public enum Int {
    ONE(1),
    TWO(2);

    private final int value;

    Int(final int value) { this.value = value; }

    public int getValue() { return value; }

    @Override
    public String toString() { return "I am integer " + value; }
}

Consider also:

public enum Operation
{
    PLUS
        {
            @Override
            public int calculate(final int i1, final int i2)
            {
                return i1 + i2; 
            }
        };

    public abstract int calculate(int i1, int i2);
}

final int ret = Operation.PLUS.calculate(2, 3);

And you can combine them both, too. They are very powerful tools.

Upvotes: 20

Related Questions