This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static variables - Please help!

 
Gary Farms
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the Compiler error?
------------------
Happy Coding,
Gregg Bolinger
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you just put your code, that isn't the main, into class (another file), you will not have these problems.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic