File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes SCJP by KS & BB: Static access to non-static enum Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "SCJP by KS & BB: Static access to non-static enum" Watch "SCJP by KS & BB: Static access to non-static enum" New topic
Author

SCJP by KS & BB: Static access to non-static enum

Matt Vanecek
Greenhorn

Joined: Oct 30, 2008
Posts: 3
Decided to get certified, so picked up this book yesterday. I have a couple questions on the self-test in Chapter 1 that didn't seem to be covered in the chapter:

1. ST question 4:


My assumption was that TestEnum.a would be null, since it is not initialized. Any other static field would be null, per default initialization. However, aside from static access using a non-static reference (a.DOG instead of Animals.DOG), this code works. Are statically-declared enums treated differently by Java? Where is this documented?

2: ST Question 9:

How is TestDays.main() able to access Days, when Days appears to me to be an instance declaration?

I love enums, but have not ever used them in a way that is contrary to normal Java access rules (e.g., can't access non-static fields from a static method, etc.) Is this contradiction further explained in the rest of the book, or am I missing something (seriously)?

Thanks,
Matt
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

For the first one, let me show you what the code of the enum will look like after compilation



So as you can see, DOG, CAT, FISH are static constants of the Animals class itself. And I think that you know that you can access static members of a class using a null reference of the class. Look at the following example



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Matt Vanecek
Greenhorn

Joined: Oct 30, 2008
Posts: 3
Thank you, Ankit! That explains the first one.

For the second one, I'm assuming the spec makes some special provision for enums declared inside a class? For example, for a normal non-static inner class, if I tried to instantiate the inner class directly from main(), I'd get a "No enclosing instance of type OuterClass is accessible." error. Inner classes also can't have static members, etc., which means in main() I can't do "InnerClass.someStaticMethod()", because the thing wouldn't compile anyhow with static fields in InnerClass.

But it seems enums are special, in that I can create an enum as an instance enum, yet can still access the instance enum in a static manner because the Java compiler makes almost everything inside the enum static, and anything inside the enum is only accessible via one of the static enum fields (e.g., Days.WED)--which can only be accessed statically via Days....or something like that.

I guess it's safe to say that writing an enum definition as a non-static "inner class" (so to speak) still leaves the enum class accessible in a static manner, where declaring a field of type "enum" follows the normal rules of access, as do any other non-static inner classes.

Somewhat circular and takes a bit of thinking...I *think* I understand this twist. My enums have always been stand-alone, because they're used across multiple classes, so I've never encountered this twist before.

Thanks,
Matt
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Well an enum inside a class doesn't have an instance of enclosing class associated with it...That's why you can use it from static methods....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SCJP by KS & BB: Static access to non-static enum