• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Inheritance | Static variable

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Greenhorn
Posts: 24
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jack Tol
Greenhorn
Posts: 24
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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
Posts: 24
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
Android Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jack...I agree with you. At the moment there is NO official documentation to argue 'otherwise'.

Cheers

Ikpefua
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic