• 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 (not) only in class?!

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello together,
I have a question about static. In most places it says, a static variable or method only exists in the class, there it is declared. Really?

Hm, the following does not agree with this:

package abcde;

public class Bird {
protected static int referenceCount = 0;
public Bird() {
referenceCount ++;
}
protected void fly() {}
static int getRefCount() {
return referenceCount;
}
}
package abcde;

class Parrot extends abcde.Bird {
public void fly() {}
public int getRefCount() {
return referenceCount;
}
}

The compiler says, that it's an error to overwrite an existing static method in a upper class. In a test, we wrote:

Parrot.getRefCount();

...and we delete the method GetRefCout in the subclass Parrot. The Compiler agree with this. Why? If the Statement would be right, the compiler have to say 'Error'.

Can someone explain this?

Greeting from Germany
MaBu
 
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't exists only to the class, it is bound to it. An object can access everything about its class, including static members.

You could create 100 instances of that class, but there would still only be one place in memory for referenceCount.

Hopefully that helps make it a bit clearer for you.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Markus,

.. a static variable or method only exists in the class, there it is declared.



This means you can not override a static method, which youjust saw when you defined the method getRefCount()in Parrot, a sub class of Bird.

When you deleted this method from Parrot, and called:

Parrot.getRefCount();

no error occurred because Parrot, the derived class inherited its behaviour (all methods) from its parent class, Bird.

Parrot also inherited the static member variable referenceCount from Bird.

And because the method getRefCount() is defined as static in Bird you were able to invoke the same using the sub class name Parrot, without instantiating Parrot.

Hope this helped you..

Himadri
 
H. J. Yoshi
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i took so long in drafting my first reply to this forum..

But anyways, I did, right..
Am sure will learn a lot among so many great people..
Hats off to the Ranch and the Ranchers!
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

You can not override a static method, which youjust saw when you defined the method getRefCount()in Parrot, a sub class of Bird.

When you deleted this method from Parrot, and called arrot.getRefCount();

no error occurred because Parrot, the derived class inherited its behaviour (all methods) from its parent class, Bird.

Parrot also inherited the static member variable referenceCount from Bird.

And because the method getRefCount() is defined as static in Bird you were able to invoke the same using the sub class name Parrot, without instantiating Parrot.

Hope this helped you..

Neerav
reply
    Bookmark Topic Watch Topic
  • New Topic