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

static

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can some please tell me whether
A static variable be inherited or not in the subclass
Thanx you
Raj
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Raj,
As per my understanding any member (field or method) is inherited unless it is
1.private in the superclass
2.hidden in the subclass OR
3.overridden in the subclass.
Since fields can only be hidden, the static field is inherited if it has not been declared as private in the superclass and also not hidden by another field with the same name in the subclass
Eg:
class Base
{
private static int i; \\not inherited since it is private
public static int j; \\inherited since it is not private
public static int k; \\not inherited since it has been hidden
}
class Derived extends Base
{
public static int k; \\hides the field with the same name in the superclass
}
Hope that helps.
 
blacksmith
Posts: 979
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ray,
Static variables will be inherited unless
they have a private access modifier.
Maybe the following example can help a little:

As you can see you don't even have to have an instance of
the KidStatic in order to access the static variable.
Greetings,
Gian Franco
[ January 29, 2004: Message edited by: Gian Franco Casula ]
 
Friends help you move. Good friends help you move bodies. This tiny ad will help:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic