my dog learned polymorphism*
The moose likes Java in General and the fly likes Inner Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic
Author

Inner Classes

mridul das
Greenhorn

Joined: Jul 15, 2005
Posts: 19
In the following code
class outer{
static int a;
static final int b;
static final int c=1;
static{
b = 2;
}

class inner{
static final int x = a;
static final int y = b;
static final int z = c;
}
}
I get 2 errors
global.java:10: inner classes cannot have static declarations
static final int x = a;
^
global.java:11: inner classes cannot have static declarations
static final int y = b;
^
Why is it an error to initiallise a static final field in an inner class to a static field or final field in the enclosing class when both are actually accessible from the inner class. Moreover static final int z = c; is ok as I have initiallised the static final field c in its declaration.But static final int y = b; gives an error when I have initiallised the static final field b in the static block of the enclosing class.
Please explain.Could you give me the logical reasoning as to why was this done.I mean the thought behind this design.
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

nothing to do with initialization. cant have static variables in inner classes.
mridul das
Greenhorn

Joined: Jul 15, 2005
Posts: 19
we cant have static variables in an inner class but static final variables are allowed.As you can see static final int z = c; compiles.But not
static final int x = a;
static final int y = b;
The problem is with innitiallisation.The way we are initiallising the final static variables x and y.
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
the first problem with initialization is:

1. You cannot use a static int final variable without initialization
(This RULE holds true even if you are not using inner class concept etc).


But why the non final static variable which has been initialized in the outer claess - giving error on being used in the inner class - but does not give error on being used by the outer class for the initialization of final static variables ? This doubt remains...

:|
Kitty


/** Code speaks louder than words */
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"Kitty Creation",
We're light on rules at the Ranch, but one we yake very seriously is the one about display names.

Display names must be two words: Your first name, a space then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name. Accounts with invalid display names get deleted, often without warning.

thanks,
Dave
[ July 31, 2005: Message edited by: David O'Meara ]
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
I am surprised abt the policies on Java ranch -

first it asks users to use complete words like "you" instead of "u" and now here the fictious name story - I wonder what more is ahead?.

Not even sun's java forums have such rules.

However since I find this forum par excellent. I am ready to oblige with its rules.

Regards.
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
The rules aimed at using proper English are well intentioned but unecessary and in fact a burden for those who aren't particularly adept typists. As for the name rule, it's also well intentioned but rather myopic and provincial: is Jan Ilsa Gumbhat Norwegian for Java Blows Chunks? Also, if they don't particularly like you and find your name the least bit whimsical, even a real one like mine, they may use it as a child-like weapon.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

We have a specific forum -- the JavaRanch forum -- for discussing this site and its rules. If you have a complaint, or would like to continue this discussion, please do so there.
[ August 07, 2005: Message edited by: Ernest Friedman-Hill ]

[Jess in Action][AskingGoodQuestions]
 
 
subject: Inner Classes