wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes static variables - Please help! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static variables - Please help!" Watch "static variables - Please help!" New topic
Author

static variables - Please help!

Gary Farms
Ranch Hand

Joined: Jun 24, 2000
Posts: 57
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);
}
}
}
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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).]


GenRocket - Experts at Building Test Data
Gary Farms
Ranch Hand

Joined: Jun 24, 2000
Posts: 57
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
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]

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

What is the Compiler error?
------------------
Happy Coding,
Gregg Bolinger
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

If you just put your code, that isn't the main, into class (another file), you will not have these problems.


Please ignore post, I have no idea what I am talking about.
Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

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
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
 
 
subject: static variables - Please help!
 
Similar Threads
About inner class declared in staic block
Call by Value
What Is This Program Doing?
? from a newbie
Why This compiler error?