This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inheritance | Static variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inheritance | Static variable" Watch "Inheritance | Static variable" New topic
Author

Inheritance | Static variable

Puja Sinha
Greenhorn

Joined: May 25, 2011
Posts: 27
Hi,

Are the static variables inherited by the subclasses. I know for sure that the static methods are not inherited but what about the static member variables of a class ?

Regards,
Pooja

Jack Tol
Greenhorn

Joined: Oct 27, 2009
Posts: 24

The same goes for static variables. After all, static methods and variables belong to a specific class.

As with static methods, you can call a static variable of a superclass directly:

The compiler takes care of selecting the static variable in the superclass.

But when you use:

it will give a error warning, seeing as i is not a part of the B class.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Hello guys,
(to the best of my knowledge) the ONLY members of a class you CANNOT inherit are constructors and members that are marked with the 'private' modifier, you can INHERIT static variables AND static methods, observe the following programs CAREFULLY:


And


Output:
20
Okay

You can crosscheck and run the program in your systems. Take note of this for the purpose of the exams you are ALMOST certain to get this kind of question.

Regards

Ikpefua.

OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
Jack Tol
Greenhorn

Joined: Oct 27, 2009
Posts: 24

My bad. In my previous post I said, this.i would give an error, but it is a warning.

However, static members are not inherited. When you look at the code you gave, you will see two warning messages:
The static field Jack.i should be accessed in a static way
The static method go() from the type Jack should be accessed in a static way

In your case the compiler just translated your calls to Jack.i and Jack.go(). Nothing was inherited.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

@Jack can you please post your complete code here, lets see why it provoked a compiler error?

Another thing I want to CLARIFY is that polymorphism (in terms of inherited members 'usage') ONLY works with instance 'non-static' methods, NOTHING more. I am sure that is where the confusion is, because it will look like inherited static members were NOT inherited in the first place.
Jack Tol
Greenhorn

Joined: Oct 27, 2009
Posts: 24

Ikpefua Jacob-Obinyan wrote:@Jack can you please post your complete code here, lets see why it provoked a compiler error?

Another thing I want to CLARIFY is that polymorphism (in terms of inherited members 'usage') ONLY works with instance 'non-static' methods, NOTHING more. I am sure that is where the confusion is, because it will look like inherited static members were NOT inherited in the first place.


@Ikpefua, I have pasted your code in Eclipse.

Please also see this topic.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Jack...Thanks for your argument, now what I can confirm at the moment is that static members are in a 'sense' inherited, but NOT that same 'strong sense' that instance variables and methods are.
An evidence is that you CANNOT 'override' a static method, however it CAN be 'redifined'.

Now as a result of what you said, I am convinced that if we are asked in the exams the answer should be that static members are NOT inherited or else otherwise proven.

The compiler indication dissappears from my program if you do this:
Jack Tol
Greenhorn

Joined: Oct 27, 2009
Posts: 24

Ikpefua Jacob-Obinyan wrote:Jack...Thanks for your argument, now what I can confirm at the moment is that static members are in a 'sense' inherited, but NOT that same 'strong sense' that instance variables and methods are.
An evidence is that you CANNOT 'override' a static method, however it CAN be 'redifined'.

Now as a result of what you said, I am convinced that if we are asked in the exams the answer should be that static members are NOT inherited or else otherwise proven.

The compiler indication dissappears from my program if you do this:


Indeed, that's the right way to access statics , via its class instead of via an indirect reference to the class.

However, the reason why you can't override a static method is because the JVM wouldn't know what to do. A static method implies that you don't need an instance of the class to access it. If it were possible to override a static method with an instance method, then what? The class which overrides would advertise indirectly that static method X is available. But you just made this an instance method, so no accessing without an instance.. A bad situation indeed.

"Overriden" in this context seems to imply inheritance, but to my idea this isn't true. It just implies that it isn't possible to use a name due to a conflict with another defined name. Maybe another name for "overriden" would be more appropriate in this context.

Please correct me when I'm wrong.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Now according to the JLS, I just found out that there are circumstances where static members can be inherited please checkout this LINK when you are there, go to section 8.1.3 it says quote
Inner classes may INHERIT STATIC MEMBERS that are not compile-time constants even though they may not declare them

If the Java Language Specification says this, there is every logic in what I said, which I will repeat here. Quote
static members are in a 'sense' inherited, but NOT that same 'strong sense' that instance variables and methods are.

When you use the keyword 'extends' it means 'inheritance' and if as a result you have access to static members of the superclass it is 'more or less' in the context of inheritance.
This is my point of view.

Regards

Ikpefua.
Jack Tol
Greenhorn

Joined: Oct 27, 2009
Posts: 24

Ikpefua Jacob-Obinyan wrote:Now according to the JLS, I just found out that there are circumstances where static members can be inherited please checkout this LINK when you are there, go to section 8.1.3 it says quote
Inner classes may INHERIT STATIC MEMBERS that are not compile-time constants even though they may not declare them

If the Java Language Specification says this, there is every logic in what I said, which I will repeat here. Quote
static members are in a 'sense' inherited, but NOT that same 'strong sense' that instance variables and methods are.

When you use the keyword 'extends' it means 'inheritance' and if as a result you have access to static members of the superclass it is 'more or less' in the context of inheritance.
This is my point of view.

Regards

Ikpefua.


Nice finding . I didn't know statics were inherited in inner classes. That's something new to look into .

However, I still believe that no form of inheritance of statics apply to top-level classes, as evident by the warning messages you got earlier. It's just the compiler translating your statements to Class.staticName.
Ikpefua Jacob-Obinyan
Ranch Hand

Joined: Aug 31, 2010
Posts: 394

Jack...I agree with you. At the moment there is NO official documentation to argue 'otherwise'.

Cheers

Ikpefua
 
Consider Paul's rocket mass heater.
 
subject: Inheritance | Static variable