aspose file tools
The moose likes Beginning Java and the fly likes Confusion on enum selection Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Confusion on enum selection" Watch "Confusion on enum selection" New topic
Author

Confusion on enum selection

Steve Bret
Greenhorn

Joined: May 03, 2006
Posts: 6
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)
}
}
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
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

Joined: May 03, 2006
Posts: 6
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

Joined: Jul 11, 2001
Posts: 14112
Sorry, I misread the nesting of your classes. It helps if you follow the advice at http://faq.javaranch.com/view?UseCodeTags
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 244
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusion on enum selection
 
Similar Threads
Object class.
Enum problem/error
need enum concept
Getting a enum based upon one of its fields
about enum