• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Enum withinInner Class

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am intermediate user of Java language. I have this interesting question (atleast for me),

why isn't it possible to declare an enum within an inner class..

for ex:

public class MyClass{
class MyInner{
enum MyEnum {} // Error unable to declare member level enum.
}
}

I assume, that it is because of the fact that Enum in java is implicitly static. But still, while you can have a "static instance variable" inside an inner class, why can't you declare an "static memeber class".

Is it not a good practice to do so?

Expecting someone to help me out on this.

Thanks
Sathiya
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is a "static instance variable"? That sure sounds like an oxymoron and Google can't find it anywhere in the JLS.

Regardless, an inner class cannot have a static member declaration unless it is a compile-time constant field. This prohibits static member class declarations and that's exactly what an enum is.

Edited for clarity.
[ May 22, 2006: Message edited by: Ken Blair ]
 
Sathiya Sun
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response Ken.

> What is a "static instance variable"? That sure sounds like an oxymoron and Google can't find it anywhere in the JLS.

Sorry I was wrong, it is "static member variable" not "static instance variable".

From you reponse I understood that, Enums, are not compile time constants.

so, what it means is..

class MyInnerClass {
static string string; // Ok
enum MyEnum {}; // Not Ok - Because it is not a compile time constant.
}

Am I right...

Thanks for explaining !
Sathiya
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sathiya Sun:
Sorry I was wrong, it is "static member variable" not "static instance variable".

From you reponse I understood that, Enums, are not compile time constants.

so, what it means is..

class MyInnerClass {
static string string; // Ok
enum MyEnum {}; // Not Ok - Because it is not a compile time constant.
}

Am I right...

Thanks for explaining !
Sathiya



You are right that "MyEnum" is not a compile-time constant field and therefore not allowed. You are wrong that the first line is okay. It's not, it's a compile-time error as well because it is not a compile-time constant either.



If you have questions on what constitutes a compile-time constant field then I will be happy to explain.
 
Sathiya Sun
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ken, It will be great if you could give me some brief explanation on what constitiutes compile time constants.

why "aString" is not okay ? My IDE doesn't show any error for the declaration.

class MyInnerClass {
static String aString; // NOT okay
}

afterall, it is static member variable and it should be assigned default value when the class is loaded, in this case "null";

Correct me if am wrong.

Thanks
Sathiya
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whether or not it is initialized to a default value is irrelevent. By definition it is not a compile-time constant.


15.28 Constant Expression


ConstantExpression:
Expression


A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:

* Literals of primitive type and literals of type String (�3.10.5)
* Casts to primitive types and casts to type String
* The unary operators +, -, ~, and ! (but not ++ or --)
* The multiplicative operators *, /, and %
* The additive operators + and -
* The shift operators <<, >>, and >>>
* The relational operators <, <=, >, and >= (but not instanceof)
* The equality operators == and !=
* The bitwise and logical operators &, ^, and |
* The conditional-and operator && and the conditional-or operator ||
* The ternary conditional operator ? :
* Parenthesized expressions whose contained expression is a constant expression.
* Simple names that refer to constant variables (�4.12.4).
* Qualified names of the form TypeName . Identifier that refer to constant variables (�4.12.4).

Compile-time constant expressions are used in case labels in switch statements (�14.11) and have a special significance for assignment conversion (�5.2). Compile-time constants of type String are always "interned" so as to share unique instances, using the method String.intern.

A compile-time constant expression is always treated as FP-strict (�15.4), even if it occurs in a context where a non-constant expression would not be considered to be FP-strict.

Examples of constant expressions:

true
(short)(1*2*3*4*5*6)
Integer.MAX_VALUE / 2
2.0 * Math.PI
"The integer " + Long.MAX_VALUE + " is mighty big."


[ May 22, 2006: Message edited by: Ken Blair ]
 
Sathiya Sun
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks ken. I have to do some howe work on that.

By the way, do you the answer what is the reason that only compile time constants are allowed inside inner classes.

Thanks
Sathiya
 
Sathiya Sun
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I confused myself with the Nested Top level class with other inner class types(regular, anonymous and local member inner class).

The bottom line is that, I can have "static members" declared within Nested Top level, but not in other non-top level classes(unless it is compile time constant), this explains why I cannot have enums declared with the regular inner clasess (not static or top level).

ested Top Level class allows for static member declarations irrespective of whether it is a "Compile Time Constant" or not.

Thanks Ken for explaining.

- Sathiya
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sathiya Sun:
I confused myself with the Nested Top level class with other inner class types(regular, anonymous and local member inner class).

The bottom line is that, I can have "static members" declared within Nested Top level, but not in other non-top level classes(unless it is compile time constant), this explains why I cannot have enums declared with the regular inner clasess (not static or top level).

ested Top Level class allows for static member declarations irrespective of whether it is a "Compile Time Constant" or not.

Thanks Ken for explaining.

- Sathiya



There is no such thing as a "nested top level" class. A nested class is a class whose declaration occurs in the body of another class. A top level class is a class that is not a nested class. An inner class is a nested class that is not static. Examples:



TopLevel and AnotherTopLevel are not declared within any other class, hence they are top level classes. NestedClass and InnerClass are both nested classes because their declarations occur within the body of another class. InnerClass, however, is even more special because it is non-static which means it is not just a nested class but an inner class as well.

Both NestedClass and InnerClass are considered members of TopLevel. NestedClass is static, making it a static member class of TopLevel. InnerClass is non-static, making it an instance member class of TopLevel.

Now consider that an "enum" is really a full fledged class that is implicitly static. If you declare that enum in TopLevel then it is a static member class of TopLevel. That's okay because TopLevel can have static members, however, InnerClass cannot have static members. If we were to declare an enum within InnerClass it would not be allowed because the only static members allowed in an inner class are compile-time constant fields. It is not a field and it certainly isn't a constant expression, so it's not allowed.

As to why this requirement is necessary, I'm not sure. I don't know if it was to keep the language simple and avoid the potentially misleading and confusing behavior or if it is simply necessary due to how inner classes must be compiled.
 
Sathiya Sun
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ken, your last post help solve a lot of mystery around this nested and inner clasess.

I came to know about the "Nested Top level" when I was searching for answers.

For Instance:

Developer forum

May be I have misinterpreted. Thanks for the patient explanation all along.

Sathiya
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sathiya Sun:
Ken, your last post help solve a lot of mystery around this nested and inner clasess.

I came to know about the "Nested Top level" when I was searching for answers.

For Instance:

Developer forum

May be I have misinterpreted. Thanks for the patient explanation all along.

Sathiya



You either misinterpreted or they were just flat out wrong. The JLS is very explicit about it. The very definition of a "top level class" is for all intents and purposes (that I am aware) "not a nested class".
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the JLS:

A nested class is any class whose declaration occurs within the body of another class or interface. A top level class is a class that is not a nested class.



I'll acknowledge the fact that the author of that article may be having the discussion in some other context and I'm just missing it. That being said, after having briefly read through part of that article they don't appear to actually have any idea what they're talking about or what the terms actually mean. I would advise that you forget everything in the article.
 
Sathiya Sun
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ken, after you pointing out, I started to look for the JLS as my ultimate reference.

I think it is wise move to start using JLS from now on.

Thanks
Sathiya
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sathiya Sun:
Ken, after you pointing out, I started to look for the JLS as my ultimate reference.

I think it is wise move to start using JLS from now on.

Thanks
Sathiya



Be careful about how 'ultimate' you consider it. It is written by humans after all and as such has it's mistakes, but it is about as authorative as it gets on Java. Don't make the mistake of assuming that outside of that context all of the terms it uses are defined the same way or that it's necessarily "right". In this context, however, the author of the article is using the terms grossly inaccurately in the context of Java and the JLS is the definitive source on what those terms are defined to mean in Java.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be fair, the term "nested top-level class" was the official and authoritative term for this time of class, for a time. When inner and nested classes were first introduced the Java, they weren't covered by the JLS (1st edition) at all. They were described by what was called either the Inner Classes Specification, or the Nested Classes Specification, depending where you read it from. While the JLS is not perfect, the Inner/Nested document was... much farther from being perfect. Anyway, it was nonetheless the official source for info on nested classes. And unfortunately, they chose "nested top-level class" as the way to decribe static nested classes. This extreme lapse in judgement was corrected when JLS second edition was released, and there was much rejoicing. Except far too many people had learned the earlier terminology at that point, and it's taken some time to retrain them.

So, when you encounter the term "top-level nested cass", be aware that it was the official term for a while, but isn't any more. For about six years, I think.

Now if you encounter someone talking about "static inner classes" - such terminology was never correct, and those people really have no excuse.
 
Sathiya Sun
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Should I follow the JLS (third edition). Now, I am bit paranoid about JLS, with all these facts.

Or is there any good book for J2SE 5 at an advanced level, that covers all these topics. To this day I haven't followed any book.

Help me guys.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, there is no perfect answer to your question. The JLS is about as authoritative as you can get, but sometimes it's still ambiguous or wrong. Don't be trapped into thinking it's going to be perfect. Testing things yourself with the JDK is a good way to find out if you're not sure about something. Beware however that the JDK is not perfect either. Anytime the JLS and JDK are telling you the same thing, you can be pretty confident in your answer. If they disagree - well then it's hard to say what's right. Sometimes it's a bug in the JDL that needs fixing; other times it may be a fundamental ambiguity in the JSL. At some point, you just have to think for yourself in interpreting things.

All of which is somewhat beside the point here, in my opinion, asa term like "top-level nested class" should be regarded as just plain wrong by modern standards. I was just commenting to express sympathy because the problem originally came from Sun. Nonetheless, Sun has fixed this problem, and I think that people clinging to archaic and confusing language need to move on.
[ May 23, 2006: Message edited by: Jim Yingst ]
 
Sathiya Sun
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks fot the explanation Jim, that makes sense.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jim Yingst:
So, when you encounter the term "top-level nested cass", be aware that it was the official term for a while, but isn't any more. For about six years, I think.

Now if you encounter someone talking about "static inner classes" - such terminology was never correct, and those people really have no excuse.



I see. Thanks, this is precisely why I wanted to say up front it was possible there was some other context in which the article would be accurate. I looked for a date but didn't see anything other than a 2003 copyright which led me to believe it was recent enough that the author ought to have known better.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sathiya Sun:
Should I follow the JLS (third edition). Now, I am bit paranoid about JLS, with all these facts.

Or is there any good book for J2SE 5 at an advanced level, that covers all these topics. To this day I haven't followed any book.

Help me guys.



I agree with everything Jim said. You can rely on the JLS as an authorative source on Java, just don't assume it's perfect. More to my point, don't assume it applies to any context outside of Java. It is not the authorative source on what's 'right' and what's 'wrong' for programming in general and the terms it uses do not necessarily have the same meaning or definition outside of Java. For example, a reference in C++ is quite a different thing from a reference in Java.
 
Sathiya Sun
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Right Ken, I was looking for some authoritative source for Java. What you explained complements what Jim said.

I started to refer JLS and as you said I can't find any "Top Level Nested Class". But being an amateur, I was pointed to different concepts and facts in the web, when desperately trying to find some answers.

It is not always poosible to read JSL to learn something, thats why I was wondering if I could get some good Java 5 reference book.

Anyway, now I started to refer JLS. I am trying to come up with an simple example that has all type of nested classes and whats possible and whats not. And I hope you guys might be able to validate my example.

Thanks guys !
 
"To do good, you actually have to do something." -- Yvon Chouinard
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic