• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static Nested Classes  RSS feed

 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't seem to get Oracle's example of creating an object in the for the static nested class to work.   Can someone give me a better example?   How can you access methods and variables contained inside a nested static class from the outer most class?

https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Here are  my two attempts to try and figure out how to use this.  


I have no issue with the nestedObjectOuter variable but when I try and use it with a dot operator it doesn't have any options. 




Here I can reach one nested static class from another nested static class but not from the outer class.


public class StaticNestedClassPractice {

int age;
static String bark = "Woof!";
static String meow ="Meow!";
String name;


static public class Dog {

public static void main(String[] args) {
System.out.println(bark);
Cat.Location();

StaticNestedClassPractice Dog1 = new StaticNestedClassPractice ();
Dog1.name ="dug";

}
}

   static private class Cat {
  
public static void Location(){
System.out.println("this is the Cat class");
}
   }

   StaticNestedClassPractice.Cat Cat1. // not able to reach Location method here
  
  
}

 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whoops here

 
Junilu Lacar
Sheriff
Posts: 11486
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Always consider context, even for examples. The examples you have don't provide a very good context for using static nested classes. The tutorial you cited gives some reasons for creating nested classes but the examples they give fail to give a good context that helps you relate to those reasons.

A better example can be taken from the standard library is Map and Map.Entry, although these are interfaces. AbstractMap and AbstractMap.SimpleEntry are associated implementation classes that reflect the relationship between Map and Map.Entry. 

Try to think of other pairs of objects that would logically go together, like maybe a Tree and Tree.Node.
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Always consider context, even for examples. The examples you have don't provide a very good context for using static nested classes. The tutorial you cited gives some reasons for creating nested classes but the examples they give fail to give a good context that helps you relate to those reasons.

A better example can be taken from the standard library is Map and Map.Entry, although these are interfaces. AbstractMap and AbstractMap.SimpleEntry are associated implementation classes that reflect the relationship between Map and Map.Entry. 

Try to think of other pairs of objects that would logically go together, like maybe a Tree and Tree.Node.


Thanks Junilu.  I see what you mean.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:. . . pairs of objects that would logically go together, like maybe a Tree and Tree.Node.
A tree and a node are logically related, so that is a good example, but I might make Node a private inner class because I can't envisage accessing it from outside the Tree.
Am I mistaken there? Trees and Nodes “logically go together” because we can say that a Node is part of a Tree.
 
Junilu Lacar
Sheriff
Posts: 11486
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Map.Entry often gets used by client code to get to the key and value of a specific map entry. I imagine a Tree.Node could also be used in much the same way, particularly if you can do something like tree.trim(node)
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That gives you several options:-
  • 1: Make Node private and use an Iterator to find where to prune the tree. That is what many linked lists do.
  • 2: Make Node private and use a value to find where to prune the tree.
  • 3: Make Node public but with a private constructor and use a value to find where to prune the tree.
  • ...and all sorts of other such combinations. Each of those is a different design and some might answer your current requirements better than others.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!