I was looking at the Java code for LinkedList and noticed that it made use of a static nested class, Entry.
public class LinkedList<E> ... {
...
private static class Entry<E> { ... ...
|
I have inherited code which contains static nested classes as:
public class Foo {
// Foo fields and functions
// ...
private static class SGroup {
...
|
It seems to me that non-public top-level classes and static nested classes essentially perform the same tasks when creating a helper class.
A.java
public class A
{
public static main ...
|
Given what I know of every other type of static feature of programming––I would think the answer is 'no'. However, seeing statements like OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass(); makes me wonder.
... |
I have been give a jar file to use that has a static inner class inside of another static inner class:
package externalJarFile;
public class Job
{
public static class GlobalVars
{
...
|
This has been driving me nuts for the past hour. I have two computers, one I work on primarily running linux mint 11 and the following version of the JDK:
java version ...
|
My question is: it's known that nested class cannot have a static member (only if the class isn't described as static). But sometimes we need to have it.
Ex: we have a ... |
|
|
Hi there, reading the Java Language Specification in preparation for the SCJP2 exam I came upon a question about nested classes. There is written: 8.1.2 Inner Classes and Enclosing Instances An inner class is a nested class that is not explicitly or implicitly declared static. Can anyone give me an example for a nested class that is implicitly declared static? Thanks ... |
|
heyas, your problem is Test.inner i = t.new inner(); is attempting to create a new instance of a static class, which defeats the actual purpose of STATIC. a static class is only created once, despite the number of instances created of your outer class.. just use Test whatever = new Test(); System.out.println(new inner().x); hope this helps -twans class Test { static ... |
Hi! Here is the code: public class StackOfInts { private static class Node { int item ; Node next ; } private Node top ; private void push( int N ) { Node newTop ; newTop = new Node() ; newTop.item = N ; newTop.next = top ; top = newTop ; } public int pop() { int topItem = top.item ... |
One reason you may nest is that the class represents a subordinate concept: public class LinkedList { private class Link { Link prev, next; Object value; } //... } Here Link is only useful in the presence of LinkedList. A second reason is hiding: you can make the nested class private, an access level that top-level classes can't enjoy (because it ... |
|
Hi All, I am studying about static nested classes and I am surprised to know that we can create instances of static nested classes. public class Test{ public static void main(String[] arg){ Test.staticTest t = new Test.staticTest(); Test.staticTest t1 = new Test.staticTest(); } public static class staticTest{ } } According to my understanding when we append static modifier that means we ... |
|
we use non static inner classes when we dont expect an object to be created independent of a specific (top-level class) object. for example : we cant have an Event Object without its associated GUI Component object.therefore its better to have an event handler within the Component. But why do we need to make an inner class static because then it ... |
|
I was perusing the Java Tutorials on Nested Classes and I came across this... [http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html|http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html] I was reading the documentation and I read a slightly confusing statement. I was hoping some further discussion could clarify the matter. The documentation on "Nested Classes" says (I highlighted the two statements, in bold, I am having trouble piecing together.) Static Nested Classes As with ... |
|