This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes enum Query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "enum Query" Watch "enum Query" New topic

enum Query

nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263

I have a query regarding enum type it may be look silly. Suppose i have defined a enum Grade in TestObj class

1. public enum Grade{
3. public int a;
4.Grade(int a){

if i try to access it in my main method. it is working fine.

1. public static void main(String[] args){
2. Grade grade=Grade.A;
3. out.println(grade.a);
4. }

Why i don't have to type
TestObj.Grade grTest=TestObj.Grade.A; why it is simply
Grade grade=Grade.A;

Thank in advance
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1710
Hi Nitin,

What is TestObj? You know in a source file, there can be only one
public class, interface or enum type. So to make your TestObj class
public, you need to keep it in separate file and your enum in another one.

If the enum is inside the class TestObj, you can access in the class,
without prefixing enclosing class name.


nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
I just want my assumptions to be correct.
Thanks chandra

Tashi Rautela

Joined: Jun 27, 2007
Posts: 22
what is the result of compiling and running the following code?
enum Level {
public void checkLevel(Level l1, Level l2) { // line 2
System.out.println(l1.toString().length() + l2.toString().length()); // line 3
class EnumFour {
public static void main(String[] args) {
Level level = Level.NOVICE; // line 5
level.checkLevel(level, Level.EXPERT); // line 6

Options :
a) error at line 2. Non static method cannot be declared with in a enum.
b) error at line 3.
c) error at line 5. assignment is not valid syntax.
d) Compiles file and Prints output 12.
e) COmpiles fine but runtime Exception will be thrown.

the ans is 'd' output 12,how has it come...
Ashu Pandey

Joined: Jul 02, 2007
Posts: 1
NOVICE length =6
EXPERT length =6
total =12

I agree. Here's the link:
subject: enum Query
It's not a secret anymore!