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;
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.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Steve Bret
Greenhorn
Joined: May 03, 2006
Posts: 6
posted
0
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;
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.
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 ]