This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one 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 The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "enum Query" Watch "enum Query" New topic
Author

enum Query

nitin pokhriyal
Ranch Hand

Joined: May 19, 2005
Posts: 263
hi,

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{
2.A(1),B(2),C(3);
3. public int a;
4.Grade(int a){
5.this.a=a;
6.}

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
Nitin
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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.


Thanks,


cmbhatt
nitin pokhriyal
Ranch Hand

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

regards,
nitin
Tashi Rautela
Greenhorn

Joined: Jun 27, 2007
Posts: 22
what is the result of compiling and running the following code?
enum Level {
NOVICE, MEDIUM, EXPERT; // line 1
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
Greenhorn

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


~Ashish
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: enum Query