I can only seem to access a static variable when I put it in a static method with a static inner class. Can't they be put in non-static methods and classes? The following program generates a compile error unless I make the inner class static and the fn method static. Why? Here's the program: public class PixelOps { public static void main(String[] args) { PixelOps ec = new PixelOps(); PixelOps.Inner mc = ec.new Inner(); // PixelOps.Inner mc = new PixelOps.Inner(); mc.fn(); } public class Inner { static int result=94; public void fn() { // int result =95; System.out.println("result = " + result); } } }
I am having a problem with your problem. What I mean is, you said that you get a compile error unless you make the fn method static. Well, even that shouldn't compile. Inner Classes cannot have static declarations. Now, the other rule is that non-static variables cannot be accessed from a static context. So, This would NOT be valid
Whereas this WOULD be valid.
Why is this? Well, to be honest, I don't know the legistics as to why. But I am sure that someone smarter than me does and will tell you once they read all this. Hope that helps a little.
------------------ Happy Coding, Gregg Bolinger [This message has been edited by Gregg Bolinger (edited November 14, 2001).]
Gary Farms
Ranch Hand
Joined: Jun 24, 2000
Posts: 57
posted
0
This program works for accesing and printing a static variable. How?? Here it is: public class PixelOps { public static void main(String[] args) { // PixelOps ec = new PixelOps(); // PixelOps.Inner mc = ec.new Inner(); PixelOps.Inner mc = new PixelOps.Inner(); mc.fn(); } public static class Inner { static int result=94; public static void fn() { System.out.println("result = " + result); } } } The version below that I sent you last time generates a compiler error. Why??
Originally posted by Gary Farms: I can only seem to access a static variable when I put it in a static method with a static inner class. Can't they be put in non-static methods and classes? The following program generates a compile error unless I make the inner class static and the fn method static. Why? Here's the program: public class PixelOps { public static void main(String[] args) { PixelOps ec = new PixelOps(); PixelOps.Inner mc = ec.new Inner(); // PixelOps.Inner mc = new PixelOps.Inner(); mc.fn(); } public class Inner { static int result=94; public void fn() { // int result =95; System.out.println("result = " + result); } } }
Gary Farms
Ranch Hand
Joined: Jun 24, 2000
Posts: 57
posted
0
Greg: I understand your remark that inner classes cannot have static declarations. This would explain why this version (where Inner is a member inner class) generates my compile error, since static int result=94; is defined globally inside it. But, why then does my other version where Inner is a static inner class (with static int result=94; defined globally) run??
Originally posted by Gregg Bolinger: I am having a problem with your problem. What I mean is, you said that you get a compile error unless you make the fn method static. Well, even that shouldn't compile. [b]Inner Classes cannot have static declarations. Now, the other rule is that non-static variables cannot be accessed from a static context. So, This would NOT be valid
Whereas this WOULD be valid.
Why is this? Well, to be honest, I don't know the legistics as to why. But I am sure that someone smarter than me does and will tell you once they read all this. Hope that helps a little. [/B]
I think for the same reason. Not only can inner class variables not be static, but inner classes cannot be static?? That is the error I get anyway. ------------------ Happy Coding, Gregg Bolinger
Right, but I think the questions is why does java not allow static inner classes and members. ------------------ Happy Coding, Gregg Bolinger
Peter Simard
Ranch Hand
Joined: Oct 31, 2001
Posts: 54
posted
0
An inner class may be declared static. A static inner class does not require an object of its outer class to be defined, where as a non-static inner class does. Also, a static inner class does not have access to the outer classes non-static members. One note re: compilation - the compiler initializes static properties first, so they are available even without instantiating an object of the defining class. ------------------ PAS peter@panvox.net 2b | | !2b
PAS<br />peter@panvox.net<br />2b || !2b
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.