File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static" Watch "static" New topic
Author

static

Raj Neets
Ranch Hand

Joined: Nov 12, 2002
Posts: 46
Can some please tell me whether
A static variable be inherited or not in the subclass
Thanx you
Raj
Sirish Kumar
Greenhorn

Joined: Jan 28, 2004
Posts: 15
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.
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
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 ]

"Eppur si muove!"
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static