• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

are static members inherited?

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are static variables and methods of super class inherited in sub class???
according to me they aren't...
But how can we use the static variables in our sub class??
Means are they visible to subclass ??
class Super
{
int i =1;
static int k = 2;
public static void print()
{
System.out.println(k);
}
}
class Sub extends Super
{
public void show()
{
System.out.println(" k : " + k);
}
public static void main(String []args)
{
Sub m =new Sub();
m.show();
print();
}
}
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So does this code compile and run? If so, what does that means with regards to your question? If not, what does that mean?

You might also play around with the access modifiers of i and k -private, protected, public- to see if that makes any difference.
 
Marshal
Posts: 79076
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More of a beginner's question.

Have you tried your two classes? Do they work?
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi

a static member is, if i may say so, a class property or behaviour.
taking an oft-repeated example, we can have an Employee class that has,
1. a static integer/long variable empId
2. a static method generateEmpId() that assigns and increments the empId whenever a new Employee object is constructed

another way to look at static members of a class is as, Global Methods or Global Constants.

think, how you would design your app ?
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i tried the above given program,i got the output as below

k:2
2


what does it prove i think definately static methods and variables can be inherited in the sub class using super class...isnt it?
please correct me if i am in a wrong direction....
 
pitambari parekh
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code runs perfectly.. And thats the reason i have a Doubt...

I'm very sure that static members are not inherited.
I just want a reason how am i able to print static members of super class in my sub class??
If i'm able to print, means these static members are visible in my sub class, means they are inherited which contradicts the statement that static members are not inhrited..
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps this old JavaRanch thread on the same topic may help you.

Are statics inherited?

Regards,
JD
 
pitambari parekh
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks John Dell'Oso ...
Just to confirm what i've understood is that(only from the accessible point of view) static variables and methods are kept with class file of super class and so are accessible in sub class...

They are not inherited ...

And when i'm printing "k" or calling "print()" in my code, internally it is read as " Super.k " and " Super.print()"

Please let me know if I'm right???
Thanks ...
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everybody...
Guys I would like to share my knowledge in java with all java lovers out there!

First of all let me tell you that static members are those members which can be accessed directly without creating an object of that particular class, when static members of a class is used in some other class then it should be used by specifying the class name .(dot) static member's name(e.g. A.i in the following example), and also if any subclass class is getting inherited from a super class which has static members and if both subclass and super class are in the same package then that static members can be accessed from the base class without even using the class name of the super class. Please go through the
Example:


Guys if you still have any confusion mail me at this email-id:
<removed>

Regards
Pradeep Kumar Tiwari




 
Sheriff
Posts: 22780
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Pradeep!

You should UseTheForumNotEmail. I have removed your email address. This will also prevent spammers from finding it.
reply
    Bookmark Topic Watch Topic
  • New Topic