| Author |
Static and Instance fields
|
Nidhi Sar
Ranch Hand
Joined: Oct 19, 2009
Posts: 252
|
|
[Another question from Niko's blog on overriding & overloading]
If there are two fields of the same name in a class, one static and one non-static, it gives an error,
so this will give an error:
class A{
Integer i = 9;
static Integer i = 777;
}
However, when it comes to class inheritance, it seems to behave differently. Please consider the following code:
This compiles and executes fine. Wouldn't Child inherit the "number" field from Parent, and effectively have two "number" fields - one static and one non-static?
|
"A problem well stated is a problem half solved.” - Charles F. Kettering
SCJP 6, OCPJWCD
|
 |
Neha Daga
Ranch Hand
Joined: Oct 30, 2009
Posts: 504
|
|
|
no this will not be a problem what you have to do is refer to the super class variable using super keyword. It is anologous to a method in a class having the same variable name as an instance variable of class then you use this keyword to refer to the instance variable.
|
SCJP 1.6 96%
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
|
This is called shadowing.
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
Nidhi Sar
Ranch Hand
Joined: Oct 19, 2009
Posts: 252
|
|
Ah.. shadowing, not overriding... ofcourse . Thanks Neha, Wouter, for helping me understand.
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
There are some rules:
1) A compilation error occurs if an instance method overrides a static method.
2) A compilation error occurs if an static method overrides a instance method.
3) It's permissible for a static variable to hide(shadow) an instance variable.
4 It's permissible for a instance variable to hide(shadow) an static variable.
Hope this will help.
And field variable are invoked on the reference type not actual object type.
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Nidhi Sar
Ranch Hand
Joined: Oct 19, 2009
Posts: 252
|
|
Abimaran Kugathasan wrote:There are some rules:
1) A compilation error occurs if an instance method overrides a static method.
2) A compilation error occurs if an static method overrides a instance method.
3) It's permissible for a static variable to hide(shadow) an instance variable.
4 It's permissible for a instance variable to hide(shadow) an static variable.
Hope this will help.
And field variable are invoked on the reference type not actual object type.
Abimaran,
That's great. It sums up the rules quite well!
|
 |
appu sharma
Ranch Hand
Joined: Sep 20, 2009
Posts: 104
|
|
Hiding Members
A subclass cannot override field of the superclass, but it can hide them. The subclass can define field with the same name as in the superclass.Code in the subclass can use the keyword super to access such member, including hidden field
If the hidden field is static, it can also be accessed by the superclass name.
|
It doesn't matter if you win by an inch or a mile; winning's winning.
|
 |
 |
|
|
subject: Static and Instance fields
|
|
|