permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inner class and inheriting static members Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner class and inheriting static members" Watch "Inner class and inheriting static members" New topic
Author

Inner class and inheriting static members

V Sa
Greenhorn

Joined: Jun 10, 2013
Posts: 4

Inner classes may not declare static members, but may inherit static members that are not compile-time constants. I do understand that there is no purely static way to access static members of non-static inner class. But what I am not able to understand is why then can that same non-static inner class inherit static members. Can anyone explain this?
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1000
    
  16

I do understand that there is no purely static way to access static members of non-static inner class.


Do you mean static and final members? Because non final static members are not allowed. When I think of it, it seems that since static members are class level members and an inner class instance has to be tied to an outer class instance ( unless we are talking of static nested classes ), it makes little/no sense to have a static inner class member unless the value of that member is not allowed to be changed.

Imagine a case of outer instance 1 tied to inner instance 1 that sets the static member to a value ( if it was allowed to change).
Outer instance 2 tied to inner instance 2 -- what should be the value for this static field if it was allowed to change... It would make it so complex, if it was doable.

Not sure if this is the correct reasoning .. will wait for others to provide an apt answer but this reasoning is what I used to convince myself.

However for the other case of inheriting a static member - it is just like static members being visible in the normal, non inner class inheritance case scenario. It is the parent class variable we are talking about. So inner subclasses can inherit them like outer subclasses.
Chan.
V Sa
Greenhorn

Joined: Jun 10, 2013
Posts: 4

Yes, I get the point regarding why static final members can be declared in non-static inner class and not static members.

From your last paragraph I understand that all the instances of the non-static inner class will access the same static member declared in the Super class. Right?

I have further query: Is there a purely static way in which these static members in Super class be accessed? (Didn't find it across the net )
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1000
    
  16
I think it is better to wait for some more knowledgeable person to respond to your post. But I tried a few things.



May be you could try all the possible combinations.

I got the following Output for the few cases I tried ( I mean the ones I actually instantiated through main).

methodLocalClassDisplay
5
5
6
6
methodLocalSubClassDisplay
7
7
8
8
outerClassMethod2Display
9
8
9
9
regularInnerClassDisplay
10
10
11
11
regularInnerSubClassDisplay
12
12
13
13

So I believe they must access the same static member-- ain't that the whole point of it being static. It is an outer class' class level field ( one copy for all instances ) after all. May be you would want to try the other types of inner classes and see for yourself. These are just the few cases that I have tried. Let us see what others have to say.

I have further query: Is there a purely static way in which these static members in Super class be accessed? (Didn't find it across the net )


I didn't understand the meaning of purely static. May be cause my knowledge is limited. But shouldn't Outerclass.varName be a static way?





V Sa
Greenhorn

Joined: Jun 10, 2013
Posts: 4

Just to bring it on the top of discussions
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18509
    
  40

V Sa wrote:Inner classes may not declare static members, but may inherit static members that are not compile-time constants. I do understand that there is no purely static way to access static members of non-static inner class. But what I am not able to understand is why then can that same non-static inner class inherit static members. Can anyone explain this?



First of all, keep in mind that "why" questions are generally answered with "because the JLS says so" or with lots of speculation (as the designers of Java are not on these forums). So, don't expect detailed concrete answers. Anyway, to speculate....

In my opinion, I don't see why a subclass (that is also an inner class) shouldn't be able to access static members of its super class. In fact, I will argue that such a restriction would probably break the ability for an inner class to also be a subclass. First, many core classes have static members; with this restriction, doesn't that mean that an inner class will be prevented from subclassing these classes? And second, if subclassing is allowed but access is not, then doesn't that restrict what the subclass can do? After all, an instance of the subclass IS-A superclass, so how can that be possible if it is not allowed to access members of its superclass?

Henry
V Sa
Greenhorn

Joined: Jun 10, 2013
Posts: 4

That sounds logical, thanks.
marton nagy
Greenhorn

Joined: Jun 07, 2013
Posts: 5

V Sa wrote:Inner classes may not declare static members, but may inherit static members that are not compile-time constants. I do understand that there is no purely static way to access static members of non-static inner class. But what I am not able to understand is why then can that same non-static inner class inherit static members. Can anyone explain this?


I think static members and methods are bound to the class where those are defined. Thus inheriting such members only means access to the static members of that particular parent. (for example if Parent has a static int i=5; this value is attached to the Parent class. every child, who inherits this, ll have access to the SAME value, or: there are only one static field for all the instances of a class and instances of subclasses. so when one child instance says i++; an instance of parent ll see 6 from it then. unless redefined of course).
In this case, we got inner classes: concept is, those classes are bound tho the outer class, as inner class instances can only exists attached to an outer class instance. wich means no static at all, and these classes also cannot be referred as static, as you said. as i see it, those "inherited" static members simply belongs to the parent class.

V Sa wrote:Is there a purely static way in which these static members in Super class be accessed? (Didn't find it across the net )


As the inner class's inherited static members are the same as those from the parent, you can access them accordingly (ParentClass.staticMember parentClassInstance.staticMember) or with the instance static trick also via inner class member (innerClassInstance.staticMember)

it became a little long sorry, but my point is: static members belong to the parent class, innerclasses cannot have static members, innerclasses can inherit statics but those belong to other classes, therefore no need to access inner's statics

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Inner class and inheriting static members
 
Similar Threads
Jxam
inner classes
inner
Question regarind Inner Classes
static modifier doubt