• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusion on enum selection  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot understand how the EARH has been selected in the first output of the main.
I have 2 earth but by the first one is selected even if 2 are available.
What is the rule for the selection of a variable?

public enum Planet {EARTH(1.00e+1, 1.0e1), MERCURY(2.2e+22, 2.2e2);
private final double mass;
private final double radius;

Planet(double mass, double radius) {
this.mass = mass; this.radius = radius;
}
public double mass() {return mass;}
public double radius() {return radius;}

public enum XPlanet {EARTH(3.0e+3), XVENUS(4.0e+4);
private final double mass;
XPlanet(double mass) {this.mass = mass;}
public double mass() {return mass;}
}
public static void main(String[] args) {
System.out.println("mass = " + EARTH.mass()); // result: 10.0 (from Planet)
System.out.println("mass = " + XPlanet.EARTH.mass()); // result: 3000.0 (from XPlanet)
}
}
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
XPlanet.main and XPlanet.EARTH are both members of the same class, so they can refer to each other without explicitely using the class name.

If you delete XPlanet.EARTH, you should instead get a compile time error, because the compiler doesn't know to use Planet.EARTH if you don't explicitely tell it to.
 
Steve Bret
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not understand you answer.
I did what you said and I can compile without problem. See below.

The question I have is still:
what is the logic for the compiler to pick up
Planet.EARTH over XPlanet.EARTH when I declare only EARTH (in the code above)?
I have 2 EARTH to choose from and they are different.
1- Why did the compiler select Planet.EARTH over XPlanet.EARTH?
2- Why is it not confused : (both enums are public)?

public enum Planet {EARTH(1.00e+1, 1.0e1), MERCURY(2.2e+22, 2.2e2);
private final double mass;
private final double radius;

Planet(double mass, double radius) {
this.mass = mass; this.radius = radius;
}
public double mass() {return mass;}
public double radius() {return radius;}

//public enum XPlanet {EARTH(3.0e+3), XVENUS(4.0e+4);
//private final double mass;
//XPlanet(double mass) {this.mass = mass;}
//public double mass() {return mass;}
//}
public static void main(String[] args) {
System.out.println("mass = " + EARTH.mass()); // 10.0
//System.out.println("mass = " + XPlanet.EARTH.mass()); // 3000.0
}
}
 
Steve Bret
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler to find out the value of EARTH is first searching through its nearest variables in this case it is first searching in Planet, where it can
find the EARTH.

Apparently it is not bothered if another EARTH is existing.

When those are declared in 2 different files (in same directory) the specification of which EARTH is required.

thanks for your anwswer.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I misread the nesting of your classes. It helps if you follow the advice at http://faq.javaranch.com/view?UseCodeTags
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Planet's main can use Planets EARTH without the need to write Planet.EARTH (because it is in the same class - Planet), but to use XPlanet.EARTH it has to write XPlanet.EARTH.
[ May 04, 2006: Message edited by: Vlado Zajac ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!