This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static and Instance fields Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Static and Instance fields" Watch "Static and Instance fields" New topic
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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Static and Instance fields
 
Similar Threads
Child extends Parent, Belongs to same package, Cannot access public variable
Using reflection to get ALL fields, not just public ones
doubt in overloading
Overriding/overloading
SCJP Help