• Post Reply Bookmark Topic Watch Topic
  • New Topic

How does enums work in this case ?  RSS feed

 
WeiJie Lim
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How does the keyword this in the CoffeeSize class refer to the size of the coffee ? I am also confused as to how the CoffeeSize constructor comes into play to determine the cost.

Thanks.



 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The enum is a class, named CoffeeSize, which contains exactly three CoffeeSize instances. As you can see the class has a constructor; the constructor is used while creating each of the three objects. The constructor gets its parameter from the definition of the enum elements. So when creating the SMALL object, the constructor is called with 5.5 as its parameter, and so on.

Likewise the instance method print() belongs to an instance of the class -- just like any other Java class, right? But in this case you have just three named instances. So when you're calling the print() method on SMALL, the "this" keyword refers to the SMALL object. That's just like any other Java object, too. And likewise the cost variable, being an instance variable, belongs to an instance of the class. So SMALL has a cost, MEDIUM has a cost, and LARGE has a cost.
 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hay WeiJie,

In the Test class you are calling orderCofee


again in the print method you are printing
So basically this refer to the current instance and here it is SMALL .
When you are passing the CoffeeSize.SMALL to the orderCofee method that time the constructor is getting called and the price 5.5 is assigned to to the cost as price of SMALL is 5.5

Hope you understand .

Satya
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing...when you try to add 'this' to the string it will use the toString() method - that's how it will work with any object. Enums automatically have a toString() method provided that will return the name of the enum value.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
S Majumder wrote:
When you are passing the CoffeeSize.SMALL to the orderCofee method that time the constructor is getting called and the price 5.5 is assigned to to the cost as price of SMALL is 5.5

That's not quite how it works. Enum constructors are invoked when the enum type is loaded by a classloader. The enum type will be loaded when a line of code that contains a reference to any of the enum values is executed. If there is no code that references any enum value, the enum type will never be loaded and the enum constructor will never be executed. When the enum type is loaded, the enum constructor will be executed for each enum value. You can see this for yourself if you put a call like this

in the enum constructor. See the difference between what happens when you comment out any lines of code that reference an enum value and when you don't comment them out.

 
S Majumder
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Junilu , for the explanation ..


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!