user13267
user13267

Reputation: 7193

Cannot understand enums in java

I was looking at an example code provided in my book

enum Coin
{
   PENNY(1),
   NICKEL(5),
   DIME(10),
   QUARTER(25);

   private final int denomValue;

   Coin(int denomValue)
   {
      this.denomValue = denomValue;
   }

   int denomValue()
   {
      return denomValue;
   }

   int toDenomination(int numPennies)
   {
      return numPennies / denomValue;
   }
}

public class Coins
{
   public static void main(String[] args)
   {
      if (args.length == 1)
      {
         int numPennies = Integer.parseInt(args[0]);
         System.out.println(numPennies + " pennies is equivalent to:");
         int numQuarters = Coin.QUARTER.toDenomination(numPennies);
         System.out.println(numQuarters + " " + Coin.QUARTER.toString() +
                            (numQuarters != 1 ? "s," : ","));
         numPennies -= numQuarters * Coin.QUARTER.denomValue();
         int numDimes = Coin.DIME.toDenomination(numPennies);
         System.out.println(numDimes + " " + Coin.DIME.toString() +
                            (numDimes != 1 ? "s, " : ","));
         numPennies -= numDimes * Coin.DIME.denomValue();
         int numNickels = Coin.NICKEL.toDenomination(numPennies);
         System.out.println(numNickels + " " + Coin.NICKEL.toString() +
                            (numNickels != 1 ? "s, " : ", and"));
         numPennies -= numNickels*Coin.NICKEL.denomValue();
         System.out.println(numPennies + " " + Coin.PENNY.toString() +
                            (numPennies != 1 ? "s" : ""));
      }
      System.out.println();
      System.out.println("Denomination values:");
      for (int i = 0; i < Coin.values().length; i++)
         System.out.println(Coin.values()[i].denomValue());
   }
}  

If enum is like a class, is it possible to have the same name for a method and field inside a class, as it has been done here with denomValue?

Why is it calling int numQuarters = Coin.QUARTER.toDenomination(numPennies); in this manner? Is toDenomination() static by default?

In the above case, what is the value of denomValue field? The output for this program hints that it should be equal to QUARTER. But how does that work? Is Coin.QUARTER equivalent to creating a coin object and passing QUARTER as it's constructor argument?

Upvotes: 0

Views: 1197

Answers (2)

Narendra Pathai
Narendra Pathai

Reputation: 41945

Each of PENNY, NICKEL are instances of Coin class and are static, so they can be accessed as Coin.PENNY.

So when you do :

Coin.QUARTER.toDenomination(numPennies)

you are accessing the QUARTER instance of coin class and calling its toDenomination() method which is an instance method being called on QUARTER instance.

When you do:

PENNY(1) //this actually calls the constructor with one integer argument of Coin class

To check that PENNY is instance of Coin class:

System.out.println(Coin.PENNY.getClass());  //Will show you Coin

Upvotes: 6

Buhake Sindi
Buhake Sindi

Reputation: 89169

Enums are special classes as:

  • They are constant classes.
  • The enum values can only be accessed statically.

Hence why Enums have a private constructor.

Once you have gotten the value, the declared methods and attributes can be accessed like an ordinary class.

the values of denomValue depends on the Enum value declared in Coin enum. For example: PENNY enum denomValue is 1 and will always be 1.

Upvotes: 1

Related Questions