• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

why can't create static nested class ?

 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,
If we define a static variable in a class then we can access it using class name or using object reference.So pls look the below code.
public class TopLevel {
public static void main(String args[]) {
TopLevel.Nested nt1 = new Nested("one");
TopLevel.Nested nt2 = new TopLevel().new Nested("two");
}
static class Nested {
String id;
Nested(String s) {
id=s;
}

public void test() {
//Nested nt = new Nested();
}
}
}
why in the above code
TopLevel.Nested nt2 = new TopLevel().new Nested("two"); is creating compile time error ? what is the problem in accessing static nested class using outer class object reference ?
Pls explain .
Raju.
:roll:
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Here is a quote from Mughal and Rasmussen, Sec 7.2: Top-Level Nested Classes and Interfaces. You can find a soft copy of this document at Programmer's Guide to Java Certification: A comprehensive primer. It says " For all intent and purpose, a top-level nested class or interface is very much like any other top-level package member class or interface. Static variables and methods belong to a class, and not to instances of class. The same is true for nested top-level classes. A static top-level class can be instantiated without any instance of the enclosing context....".
So the correct way of instantiating the Nested class should be

Hope it helps.
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A nested class is quite different from an inner class. For one thing, there is no need for an enclosing instance for a nested class, unlike an inner class which requires one.
Corey
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's a little confusing to say that a nested class is quite different from an inner class - an inner class IS a nested class. As per the JLS, any class defined within another class is a nested class, and a top-level class is any class which is not nested. Note that this renders the term "top-level nested" an oxymoron.
Using "inner" to describe those classes which require an enclosing instance of their enclosing class seems to be the most natural and useful use of the term, but the JLS throws a curve here. The JLS has it that an inner class is any nested class which is not declared static. This turns out to be every nested class other than a static member class - a useless distinction.
Actually, the JLS says: "An inner class is a nested class which is not explicitly or implicitly declared static." (8.1.2). If local and anonymous classes declared in static contexts can be considered "implicitly declared static", then the term "inner" means what we would like: needs an enclosing instance.
But the JLS nixes this in 14.3 and 15.9.5, where it states (arbitrarily?) that anonymous and local classes are always inner. It's as if the JLS forgets that local and anonymous classes can be declared in static contexts, where they have no enclosing instance. I believe this is a JLS bug. If it just didn't say that anonymous and local classes are always inner, we could use the term "inner" where we need it - or maybe people are ignoring the JLS and doing this anyway, as in inner story.
Actually, I think this problem and much of the obscurity of nested classes would disappear if inner (JLS sense) classeswere allowed to have static members, but I suppose this is another topic.
[ April 02, 2004: Message edited by: Steve Lovelace ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic