• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static and Instance fields

 
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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?
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is called shadowing.
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah.. shadowing, not overriding... ofcourse . Thanks Neha, Wouter, for helping me understand.
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Nidhi Sar
Ranch Hand
Posts: 252
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Ranch Hand
Posts: 104
Netbeans IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic