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 Why can't inner classes declare static members?* 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 "Why can Watch "Why can New topic
Author

Why can't inner classes declare static members?*

Panagiotis Varlagas
Ranch Hand

Joined: Nov 27, 2000
Posts: 233
(* ...with the exception of compile-time constants)
This seems to be a recurring topic, but I believe it has not been satisfactorily
resolved yet.
Says Bill Bozeman in a posting of his:
"Static variables are not allowed in inner
classes because you have to have an instances
of the outer class in order to access the
inner class but that goes against what static
variables do. With static variables you
don't have to have an instance of the class
to access the static variabe."
Well, inner classes may not be allowed to
declare any static members, but they
still inherit all static members of
their superclass. And those static members
can be accessed via the inner subclass
just fine without the need for any object
getting instantiated.
Here is a little sample program I wrote to
ilustrate my point:
<pre>
===================
class HasStatic {
public static void foo() {
System.out.println("I am static");
}
}
class Outer {
class Inner extends HasStatic {
}
}
class Test {
public static void main(String[] args) {
Outer.Inner.foo();
}
}
===================
</pre>
The above code compiles just fine and when
run produces the following output: "I am
static". No objects were created.
So I wonder: what harm would it make if
static members were allowed to be declared in
addition to be inherited? It would
definitely not "break" the "philosophy" of
static access.
I would guess that it is just an arbitrary
language decision; perhaps under some time
constraint it was just easier for the javac
writers to assume that inner class members
cannot be static.
Any ideas on this?
Panagiotis.

[This message has been edited by Panagiotis Varlagas (edited January 14, 2001).]
Zheng Huang
Ranch Hand

Joined: Dec 20, 2000
Posts: 49
I got the following error while compileing.
Test.java:12: Can't make a static reference to inner class Outer. Inner.
Outer.Inner.foo();
Panagiotis Varlagas
Ranch Hand

Joined: Nov 27, 2000
Posts: 233
On the other hand, in my case it compiles and runs just fine. I am using the VisualAge for Java IDE 3.5. (Java 1.2.2). Haven't tried to javac this program. Could be a difference between the javac compiler and the VAJava compiler.
Panagiotis.
Cristi Tudose
Ranch Hand

Joined: Dec 25, 2000
Posts: 53
Hi Panagiotis,
I had the same problem in understanding.
For me,the answer is at intuitive level
and I have nothing more to say that what Bill said(i see the same reason)
Your code runs corectly(JDK1.3) and states an acces of an
inhereted members from a class other than outer(fully qualify)
rgds,
Cristi
amit sanghai
Ranch Hand

Joined: Dec 05, 2000
Posts: 231

Hello guys,
I am used JDK1.2.2. It is giving the same error as in JDK1.3
:
Cant make a static reference to Inner class Outer.Inner.

If instead of extending class HasStatic I define the method foo() as static in the Inner class itself then it gives one more error :
Method foo() cant be static in inner class Outer.Inner. Only members of interfaces and top-level classes can be static.
sameer ss
Greenhorn

Joined: Jan 15, 2001
Posts: 2
Hi,
I think we must have to declear inner class as stsic then program will run.please give comments if any
Wong KK
Ranch Hand

Joined: Jan 15, 2001
Posts: 52
Hi all,
Wong KK
Ranch Hand

Joined: Jan 15, 2001
Posts: 52
Hi all,
For the mentioned programm, I find that it works fine for JDK1.3 but not for JDK1.2.2. And It also work fine in Visual Age for Java 3.5. Why will there such an ambiguous??
Panagiotis Varlagas
Ranch Hand

Joined: Nov 27, 2000
Posts: 233
Says Wong: "For the mentioned programm, I find that it works fine for JDK1.3 but not for JDK1.2.2. And It also work fine in Visual Age for Java 3.5. Why will there such an ambiguous??"
I believe that there must be a difference in the compilers that come with JDK 1.3/VAJava vs the compiler that comes with 1.2.2 in how they treat such accesses. The specification does not explictly specify whether such a kind of access should be disallowed or not; so, some compiler implementations choose to forbid it, while others to allow it.
Panagiotis.

Weigang Gu
Ranch Hand

Joined: Jan 16, 2001
Posts: 44
here is my understanding. Tell me if I am wrong.
The reason is clear why non-static inner class can not have static variables or methods. I just treat member inner-class as one of member variables/methods, and as you know you can not declare any local variables as static. (Static inner class, on the other hand, is in fact a top level class and can have static variables and methods).
If an inner class is not static, then it must require an instance of enclosing class for its behavior and that is why invoking foo() via Outer.---is wrong (because no instance of Outer is declared.) So you have to declare an instance of Outer in order for Inner to behave(to invoke its method). So you have to declare Outer outer = new Outer(); for inner to invoke foo(),
you can either do outer.new Inner().foo(); or outer.Inner.foo()
but the latter one is syntaxically wrong.
And one more thing, inside Inner class, you can invoke Inner.foo() without any problem. So the static method foo() is really hidden in the inner class Inner, and this static method is not seen by enclosing Outer class.
Any comments?
Originally posted by Panagiotis Varlagas:
Says Wong: "For the mentioned programm, I find that it works fine for JDK1.3 but not for JDK1.2.2. And It also work fine in Visual Age for Java 3.5. Why will there such an ambiguous??"
I believe that there must be a difference in the compilers that come with JDK 1.3/VAJava vs the compiler that comes with 1.2.2 in how they treat such accesses. The specification does not explictly specify whether such a kind of access should be disallowed or not; so, some compiler implementations choose to forbid it, while others to allow it.
Panagiotis.

Zheng Huang
Ranch Hand

Joined: Dec 20, 2000
Posts: 49
I compiled the code in 1.3. It worked. The same code doesn't work in 1.2. Can moderators rais this issue to some experts or Sun?
Wong KK
Ranch Hand

Joined: Jan 15, 2001
Posts: 52
For the below mentioned program, does it violate inner class's definition?
public class e {
public static void main(String args[]) {
System.out.println(f.g.i);
}
}
class f {
static class g {
public static int i = 10;
}
}
******************************************
Says Bill Bozeman in a posting of his:
"Static variables are not allowed in inner
classes because you have to have an instances
of the outer class in order to access the
inner class but that goes against what static
variables do. With static variables you
don't have to have an instance of the class
to access the static variabe."
************************************************
Panagiotis Varlagas
Ranch Hand

Joined: Nov 27, 2000
Posts: 233
Check this new thread out: <A HREF = "http://www.javaranch.com/ubb/Forum24/HTML/007432.html<br /> ">http://www.javaranch.com/ubb/Forum24/HTML/007432.html
. It deals with the issue that was created in this thread, namely the inconsistency between compilers in whether they allow access to inherited members of inner classes from outside the inner classes.
Panagiotis.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why can't inner classes declare static members?*