Re: inheritance of static attribute?

From: Babu Kalakrishnan (k.a.l.a_at_sankya.com)
Date: 08/12/04


Date: Thu, 12 Aug 2004 21:33:09 +0530

Richard Chrenko wrote:
> I have the parent class Parent in which I declare a static attribute
> theStatic, and a child class which also declares the static attibute
> theStatic. I would have thought that this should not compile since the
> child class attribute would conflict with the parent class attribute of
> the same name. However, it does in fact compile! Can the class child in
> fact access both parent.theStatic and it's own local attribute theStatic?
>
> public class parent {
> static int theStatic;
>
> public parent() {
> }
> }
>
> public class child extends parent {
> static int theStatic;
>
> public child() {
> }
> }

See section §8.3.3 of the JLS Second Edition.

It is perfectly legal for the parent class and the child class to have static
variables with the same name - even the types of the two may be different
(though probably a great idea to use this feature in your code). Both the
variables can be accessed separately by using the classname (Parent.x and
Child.x) if the access modifier of the field allows it. Also the child may
access the parent's variable as super.x (which again is bad for code
readability)

BK



Relevant Pages

  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)
  • Unix Programming FAQ (v1.37)
    ... Why use _exit rather than exit in the child branch of a fork? ... Why doesn't my process get SIGHUP when its parent dies? ... How do I create a named pipe? ... How do I compare strings using regular expressions? ...
    (comp.unix.programmer)