Re: inheritance of static attribute?
From: Babu Kalakrishnan (k.a.l.a_at_sankya.com)
Date: 08/12/04
- Next message: vnssoftware: "Connect using Java RMI to a running Java Application"
- Previous message: cppaddict: "Re: System.load() problem"
- In reply to: Richard Chrenko: "inheritance of static attribute?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: vnssoftware: "Connect using Java RMI to a running Java Application"
- Previous message: cppaddict: "Re: System.load() problem"
- In reply to: Richard Chrenko: "inheritance of static attribute?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|