Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Enum Problem

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can somebody explain the output of this program-
enum Direction {
EAST("HELLO"),WEST("HI"),NORTH("HELLO"),SOUTH("HI"){
public String toString() { return "HELLO"; }
};
String side;
private Direction(String d) {
side = d;
}
public String toString() {
return "HI";
}
}

class EnumTwo {
public static void main(String[] args) {
System.out.println(Direction.EAST + " " + Direction.WEST + " " + Direction.NORTH + " " + Direction.SOUTH);
}
}
Answer : HI HI HI HELLO
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Here we go , enum Direction has overriden toString() of Object class.
Now if you observe SOUTH("HI") , It defines a constant specific class body,
i.e, it defines a anonymous inner class that extends Direction class [enums are classes internally] and overrides toString() of Direction.
As a result
System.out.println(Direction.EAST); This will invoke toString() of Direction which will display Hi
System.out.println(Direction.SOUTH); This will invoke toString() of anonymous inner class that overrides toString() of Direction.

Thanks
Deepak
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
neha,

when you call System.out.println(Direction.EAST + " " + Direction.WEST + " " + Direction.NORTH + " " + Direction.SOUTH);

for each enum Constant their own toString() mthod will called.

As you have specifically overridden the toString method for this SOUTH("HI"). So it is displaying as " HEELO ".

for remaining enum constats this tostring mthod will be called.
public String toString() {
return "HI";
}
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy ranchers,

normally you don't override toString() in an enum. Because the method normally returns the "name" of an element of the enum.
So System.out.print(Direction.NORTH) should print "NORTH".
When - as in the example - the overridden method prints "HI", it will print "HI" for every element of the enum. Unless the method is again and individually overidden for an element. Again as in the example.

Perhaps have a look at this modified example and try to guess what it will print out.
This enum brings its own main method, so you can start it like any class that has a main-method.



Yours,
Bu.
 
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic