aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Enums... 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 "Enums..." Watch "Enums..." New topic
Author

Enums...

Higgledy Smith
Ranch Hand

Joined: Mar 07, 2006
Posts: 192
On page 63 of the Sierra/Bates SCJP 6.0 book. I coded the enum example and posted my code below. My question is; Why does the instance variable need to be decalred below the constructor, as shown in the book? I tried it both ways and got an error when it was declared above the constructor.

Thanks again.

Higgledy


Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
I think what you actually meant is that the enum constants must be declared at the beginning of the enum (before any other declarations.) And the reason why it's that way is because the Java specification says so.

From the JLS, 3rd Edition (8.9):


An enum declaration has the form:
EnumDeclaration:
ClassModifiersopt enum Identifier Interfacesopt EnumBody
EnumBody:
{ EnumConstantsopt ,opt EnumBodyDeclarationsopt }



I think it makes sense to have the constant declarations first, as that allows any other later declarations to use those constants. But I don't know if that's the exact reason why this form is required.


All code in my posts, unless a source is explicitly mentioned, is my own.
Higgledy Smith
Ranch Hand

Joined: Mar 07, 2006
Posts: 192
Ruben Soto wrote:I think what you actually meant is that the enum constants must be declared at the beginning of the enum (before any other declarations.) And the reason why it's that way is because the Java specification says so.

From the JLS, 3rd Edition (8.9):


An enum declaration has the form:
EnumDeclaration:
ClassModifiersopt enum Identifier Interfacesopt EnumBody
EnumBody:
{ EnumConstantsopt ,opt EnumBodyDeclarationsopt }



I think it makes sense to have the constant declarations first, as that allows any other later declarations to use those constants. But I don't know if that's the exact reason why this form is required.



No. I am referring to these two declarations:



But if that is the spec. then that could be a reason why I experienced a compile error.

A person learns so much from testing these examples.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Oh, OK. That's just illegal forward referencing. When you are in a constructor and you refer to an instance variable you must make sure that the instance variable has been defined before the constructor in the body of the class. There is a very nice document on forward referencing here: Forward Referencing
I don't think that document explicitly covers the constructor-instance variable case, but it works the same way.
That should clear your doubts. But, basically, it comes down to this: By the time the constructor is running, it must know what instance variables your class has.
Higgledy Smith
Ranch Hand

Joined: Mar 07, 2006
Posts: 192
Ruben Soto wrote:Oh, OK. That's just illegal forward referencing. When you are in a constructor and you refer to an instance variable you must make sure that the instance variable has been defined before the constructor in the body of the class. There is a very nice document on forward referencing here: Forward Referencing
I don't think that document explicitly covers the constructor-instance variable case, but it works the same way.
That should clear your doubts. But, basically, it comes down to this: By the time the constructor is running, it must know what instance variables your class has.


Ruben,

I am not following. I rcv'd the error only when those two instance variables were declared before the constructor. But, from your last reply it sounds like that is were they need to be declared, before the constructor. What don't I understand?
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
That's because I jumped the gun on this, and I wrote a lot of nonsense (forward referencing only applies to instance (and static) initializers and to instance (and static) initialization blocks.)

So forget what I said about the constructor, although the Forward Referencing document is very good and I recommend that you read it.

What is going on in your case? I just tried compiling and running it (after putting the instance variable declarations before the constructor) and it worked. Are you using Eclipse or something like that? I just use the javac and java command utilities.
Higgledy Smith
Ranch Hand

Joined: Mar 07, 2006
Posts: 192
Ruben Soto wrote:That's because I jumped the gun on this, and I wrote a lot of nonsense (forward referencing only applies to instance (and static) initializers and to instance (and static) initialization blocks.)

So forget what I said about the constructor, although the Forward Referencing document is very good and I recommend that you read it.

What is going on in your case? I just tried compiling and running it (after putting the instance variable declarations before the constructor) and it worked. Are you using Eclipse or something like that? I just use the javac and java command utilities.


Me too. Using TextPad, javac and java. No Eclipse. I'll try it again. Maybe I did something stupid like forgetting a semicolon? Thanks.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Yes, that could be it. Try and see if you can make it work.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Enums...
 
Similar Threads
Need a link to errata for Java 6 study Guide--Sierra and Bates
Enum programs are not compiling in java5.0 compiler?
Enum overloaded constructer
use of " this " ...
Question about enums and enhanced for