GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes non-static member class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "non-static member class" Watch "non-static member class" New topic
Author

non-static member class

Girish Nagaraj
Ranch Hand

Joined: Apr 19, 2006
Posts: 153
class Outer {

static int i = 2;

class Inner extends Outer {}

public static void main(String[] args) {

System.out.println(new Outer().new Inner().i);
System.out.println(Inner.i); // line (1)
}
}

Non static member class cannot have static declarations.
But Inner class above extends Outer which has static variable i.
And we are able to access i using static reference.How???
Amieya Prabhaker
Ranch Hand

Joined: Apr 23, 2006
Posts: 45
What it means is you cannot have something like this:

class Inner extends Outer { static int i = 3;}

This will give a compiler error.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
8.1.3 Inner Classes and Enclosing Instances
Inner classes may inherit static members that are not compile-time constants even though they may not declare them.
Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Posts: 324
Hi all

can any one please explain a bit more. I am confused on this concept


"Know where to find the solution and how to use it - that's the secret of success."
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
First of all: static members are never inherited.

Now, let's see the different ways to access a static member:



1. Using the class Jedi:

2. Using an instance of the class Jedi:

3. However, the instance of the class is not at all necessary, since the variable belongs to the class, not to the instance, hence, it could be done using a null reference of the class:

4. From within a method of the class Jedi:

All this simply proves that a variable reference can access the static members of its class or its super classes as long as they are accessible whether the variable reference has been initialized or not.

If we have a set of classes like this:



Notice that the variable name can be accessed through a Child reference, just because the Child extends the Parent class. That does not imply inheritance. Modifying the value of name in the Child class will modify the value in the Parent class. This is the proof that name actually belongs to Parent

Try it, writing a code like this:


You will see that modifying the name field from the Child class will modify the value of the Parent class. This demonstrates that there is such thing as static members inheritance.

When it comes to nested classes, specifically non-static nested classes, they cannot declare new static members, but are allowed to access the static members of the Outer class, or those accessible from its extended classes and implemented interfaces.

It is also important to set clear that non-static nested classes actually are allowed to declare static members, as long as they are final and are initialized to a constant expression. (See JLS 15.28)
[ May 27, 2006: Message edited by: Edwin Dalorzo ]
Amirr Rafique
Ranch Hand

Joined: Nov 14, 2005
Posts: 324
Thanks Edwin
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: non-static member class