• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 440
  • Last Modified:

enum in constructor

Hi,
I am trying below example

http://www.avajava.com/tutorials/lessons/how-do-i-use-the-enum-type-with-a-constructor.html ee


I see in this example code like below

 
public enum Food {
    HAMBURGER(7), FRIES(2), HOTDOG(3), ARTICHOKE(4);

    Food(int price) {
      this.price = price;
    }

    private final int price;

    public int getPrice() {
      return price;
    }
  }


Is above code represents Food constructor or enum constructor. I am not clear on that. what is the purpose of enum inside private constructor.
please advise
Any links resources ideas highly appreciated. Thanks in advance
0
gudii9
Asked:
gudii9
  • 3
  • 2
  • 2
2 Solutions
 
Ken ButtersCommented:
The enum is defining which types of food there are...

HAMBURGER
FRIES
HOTDOG
ARTICHOKE

in addition to a type of food, each of the enum values is also setting a price for each enum.

HAMBURGER(7) ~ price of hamburger = 7
FRIES(2) ~ price of fries = 2
HOTDOG(3) ~ price of Hotdog = 3
ARTICHOKE(4) ~ price of artichoke = 4

Here is link to java page that gives tutorial on enums that uses a similar example with planets:

http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
0
 
dpearsonCommented:
Is above code represents Food constructor or enum constructor.

It's actually both.

Food(int price) { ... }

Is the constructor for Food, which is itself an enum.  So it's also an enum constructor.

Usually enums are just a simple list of values: A,B,C.
This is showing how you can attach more values to an enum - in this case "price".

Doug
0
 
gudii9Author Commented:
I was reading more on constructor here
http://www.javaworld.com/article/2076204/core-java/understanding-constructors.html

It's actually both.

Food(int price) { ... }


I still not clear how it is constructor to the class since class name is EnumDemo right?
Does enum always need a constructor to use it. please advise
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
dpearsonCommented:
Let's start with a simpler example:

public class EnumDemo {
       // This is the constructor for EnumDemo
       public EnumDemo() {
       }

	public enum Food {
		HAMBURGER, FRIES, HOTDOG, ARTICHOKE;
	}
}

Open in new window


This is a single class (EnumDemo) that has a constructor and also contains an enum.

So far I assume there's no confusion.

Then we can change this into a more complex enum like this:

public class EnumDemo {
       // This is the constructor for EnumDemo
       public EnumDemo() {
       }

	public enum Food {
                HAMBURGER(7), FRIES(2), HOTDOG(3), ARTICHOKE(4) ;

                // This is a constructor for the Food enum
                // NOT the enumDemo class.
		Food(int price) {
			this.price = price;
		}

		private final int price;

                // This is a method on the Food enum
                // NOT the enumDemo class
		public int getPrice() {
			return price;
		}
        }
}

Open in new window


The constructor we added was a constructor for the Food enum.  Not for the EnumDemo class (which still has a constructor - the one we created at the start).

Does that make sense?

Doug
0
 
gudii9Author Commented:
sounds good.

for (Food f : Food.values()) {
                  System.out.print("Food: " + f + ", ");

                  if (f.getPrice() >= 4) {
                        System.out.print("Expensive, ");

where is values() method on the  Food enum defined. I do not see that.

public enum Food {
            HAMBURGER(7), FRIES(2), HOTDOG(3), ARTICHOKE(4);

How jave know the value within parenthesis like 7 in HAMBURGER(7) is referred as price.
What is the use of enum here instead of a class as we have variables and methods as well in this enum.
Please advise
0
 
Ken ButtersCommented:
A java enum is not necessarily a simple class that lists possible values in the manner that you may have become accustomed to in other languages.

It can be defined that way , but in Java, it does not have to be.

The way java knows that hamburger(7) refers to a price is because of the contructor use for each enum value.... each 'Food'.

This says that for each of the enums...
HAMBURGER(7), FRIES(2), HOTDOG(3), ARTICHOKE(4) ;

... keeping in mind that each of the above is one of the enumerations of a type of FOOD.

That the food will be defined with a constructor for the food...

Food(int price) {
                  this.price = price;
            }

So when you list "HAMBURGER(7)"

then this in turn invokes

Food(int price) {
                  this.price = price;
            }

Where price is 7.


now you have a HAMBURGER enum
            with a value of 7 stored in the "price" variable of Hamburger.

There is a separate "price" field ... one for EACH enumeration of HAMBURGER(7), FRIES(2), HOTDOG(3), ARTICHOKE(4) ;
0
 
dpearsonCommented:
where is values() method on the  Food enum defined. I do not see that.

Yeah that's good spotting.  The values() method is actually something you get for free on any enum.  You don't have to write it yourself.  A bit like how you get "toString()" on any object.

I think Ken explained how the "7" is tied into being the price.

You can compare the price example with this other piece of code, where we're instead storing the number of calories in the enum:

public class EnumDemo {
       // This is the constructor for EnumDemo
       public EnumDemo() {
       }

	public enum Food {
                HAMBURGER(700), FRIES(800), HOTDOG(450), ARTICHOKE(100) ;

                // This is a constructor for the Food enum
                // NOT the enumDemo class.
		Food(int calories) {
			this.calories = calories;
		}

		private final int calories;

                // This is a method on the Food enum
                // NOT the enumDemo class
		public int getCalories() {
			return calories;
		}
        }
}

Open in new window


Hope this is all getting clearer,

Doug
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now