• Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing non-static members of top-level static nested classes  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How could you access the instance variable i in the code below? I'm a little confused about why you could have a non-static member of a static inner class. I tried creating an instance of TopLevelClass and then referring to variable i, but I couldn't get that to work. Thanks!
public class TopLevelClass
{
static class NestedTopLevelClass
{
int i = 50;
}
public static void main(String[] arrgghh)
{
TopLevelClass tlc = new TopLevelClass();
// tlc.NestedInnerClass.i ???
}
}
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Kelly,
Try the following code and you'll get a printout of 50.
1. public class TopLevelClass{
2. public static void main(String[] arrgghh){
3. TopLevelClass tlc = new TopLevelClass();
4. System.out.println(TopLevelClass.NestedInnerClass.i);
5. }
6. static class NestedInnerClass{
7. static int i = 50;
8. }
9. }
You don't need an instance of TopLevelClass in order to access NestedInnerClass. In fact, for some reason (can somebody explain to me?), I was getting an error when I tried to replace line 4. with:
4. System.out.println(tlc.NestedInnerClass.i);
The compiler complained that: "The field named NestedInnerClass for type named test.TopLevelClass is not defined." (test is my package name)
Does this mean that you can't access a static inner class via an instance of the enclosing class?
At any rate, Kelly, to access the int i at line 7 without instanciating NestedInnerClass, you need to make i static as I did.
The reason for having non-static variables inside a static class is to be able to perform operations on those variables just like you would want static and non-static variables in TopLevelClass for various functionality. You may want int i to be used in calculations, but not accessed statically.
That's a little confusing. Basically, a static inner class allows you to create an instance of NestedInnerClass without having an instance of the enclosing TopLevelClass. Basically, this makes NestedInnerClass a "top-level class with a modified naming scheme" (RHE, 193).
Have I confused you more?
April
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi April,
I too am a greenhorn and having inner class blues myself. What I understand is the members of static inner class can either be static or non-static. As Kelly is writing an inner class with non-static member i, it can vary for each object instanciated of inner class and hence you cannot make a static reference to it.

Originally posted by April.Johnson:
Hi, Kelly,
6. static class NestedInnerClass{
7. static int i = 50;
8. }
9. }
You don't need an instance of TopLevelClass in order to access NestedInnerClass. In fact, for some reason (can somebody explain to me?), I was getting an error when I tried to replace line 4. with:
4. System.out.println(tlc.NestedInnerClass.i);
The compiler complained that: "The field named NestedInnerClass for type named test.TopLevelClass is not defined." (test is my package name)
Does this mean that you can't access a static inner class via an instance of the enclosing class?
At any rate, Kelly, to access the int i at line 7 without instanciating NestedInnerClass, you need to make i static as I did.
The reason for having non-static variables inside a static class is to be able to perform operations on those variables just like you would want static and non-static variables in TopLevelClass for various functionality. You may want int i to be used in calculations, but not accessed statically.
That's a little confusing. Basically, a static inner class allows you to create an instance of NestedInnerClass without having an instance of the enclosing TopLevelClass. Basically, this makes NestedInnerClass a "top-level class with a modified naming scheme" (RHE, 193).
Have I confused you more?
April


 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Kelly Walker:
How could you access the instance variable i in the code below? I'm a little confused about why you could have a non-static member of a static inner class. I tried creating an instance of TopLevelClass and then referring to variable i, but I couldn't get that to work. Thanks!
public class TopLevelClass
{
static class NestedTopLevelClass
{
int i = 50;
}
public static void main(String[] arrgghh)
{
TopLevelClass tlc = new TopLevelClass();
// tlc.NestedInnerClass.i ???
}
}


Since the inner class is a static class u could also print out the value of i by the following piece of Code
new TopLevelClass.NestedTopLevelClass().i;
Hope this helps
Anand
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by April.Johnson:
Hi, Kelly,
Try the following code and you'll get a printout of 50.
1. public class TopLevelClass{
2. public static void main(String[] arrgghh){
3. TopLevelClass tlc = new TopLevelClass();
4. System.out.println(TopLevelClass.NestedInnerClass.i);
5. }
6. static class NestedInnerClass{
7. static int i = 50;
8. }
9. }
You don't need an instance of TopLevelClass in order to access NestedInnerClass. In fact, for some reason (can somebody explain to me?), I was getting an error when I tried to replace line 4. with:
4. System.out.println(tlc.NestedInnerClass.i);
The compiler complained that: "The field named NestedInnerClass for type named test.TopLevelClass is not defined." (test is my package name)
April


I compiled and ran the code just fine. But, I am using JDK 1.3... maybe that's why?
-Grant
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!