Click here to Skip to main content
16,015,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I read this in a post that static variables are not inherited in Java.. I wrote a code to check for the same but I dont understand why is it inheriting.. Here is the code..

class B{
    static String name = "ABCD";
}

class Abc {
    public static void main(String args[] ) throws Exception{
        C c = new C();
        c.disp();
    }
}

class C extends B{
    void disp(){
        System.out.println(name); // Inheriting name from B
    }
}


The code prints "ABCD" on the console.. Please tell me what I am missing here..

Thanks In Advance..!

What I have tried:

I am trying to check whether static variables can be inherited or not..
Posted
Updated 6-Feb-18 4:45am
v4

Static variables in Java are not inherited, they exist only in the class which declares them; however, they can be accessed implicitly by referring to an instance or subclass (or subclass instance) of the class which defines the static variable.

So, name only exists in B, but you can also access it using C.name (so from code in the C class, just name is enough).
 
Share this answer
 
Comments
Akshit Gupta 30-Jan-18 14:35pm    
Thank you sir for reverting.. But how to differentiate whether the variable is inherited or statically called.. I mean we call the inherited variables the same way.. If I am not wrong
Thomas Daniels 30-Jan-18 14:38pm    
As OriginalGriff said in his answer:

"And inherited members can be overridden, hidden, or replaced."

That's a notable difference: you cannot do that for the 'name' variable.
Static variables in Java are not inherited, because they are a part of the class implementation, not of the instance-of-a-class implementation - they are not members of a class instance.

What that means is that a "normal" variable is part of the class instance: thisOne contains a different "copy" of a nonstatic varianle myVar that thatOne - thisOne.myVar does not contain the same number as thatOne.myVar.

But static variables are the same for all instances because they are not a part of the instance, there is one space allocated and it is referred to by all instances (or even without an instance at all).

And inherited members can be overridden, hidden, or replaced.

Which means static members can't be inherited: because the inherited version would need to potentially be different for your class C that it would be for class A and that can't happen.
 
Share this answer
 
Comments
Akshit Gupta 30-Jan-18 14:51pm    
Thank you sir.. That solved my problem..
Static method in Java is a method which belongs to the class and not to the object. A static method can access only static data.

    It is a method which belongs to the class and not to the object(instance)
    A static method can access only static data. It can not access non-static data (instance variables)
    A static method can call only other static methods and can not call a non-static method from it.
    A static method can be accessed directly by the class name and doesn’t need any object
    A static method cannot refer to "this" or "super" keywords in anyway
 
Share this answer
 
v2
Comments
CHill60 6-Feb-18 10:47am    
If you copy stuff from a page found by a google search you should credit your source otherwise you will accused of plagiarism.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900