• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

static variables and methods

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I read the discussions about hiding static methods in subclasses and I think I'm clear about it. But what happens if a static method is NOT redefined in the subclass - is it simply inherited? I got confused as I did the seft test in chapter 2 (Q.11) of the new K+B book where the main method in the subclass calls a static variable of the superclass simply by calling it by name without anything in front:

class Uber {
static int y = 2;
// more code
}
class Minor extends Uber {
// more code
public static void main(String [] args) {
// more code
System.out.println(y);
}}

I thought that would cause a compilation error but the answer is that the code gets executed. Is that so?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes you can access the static members of the superclass from the subclass unless you hide them.
 
Ranch Hand
Posts: 286
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
its so , static member aren't herited because they are hidden to the subclass so you can't override it. But you can use them...when you don't use a reference with the same name, its not a problem. If here you use another static int y , you won't be able to 'see' the Uber.y and note that you can't do something like super.y because you can't use super or this in a static context.
I hope i was a bit helpfull,
arno

class Uber {
static int y = 2;
// more code
}
class Minor extends Uber {
static int y = 1
public static void main(String [] args) {
System.out.println(y); => 1
System.out.println(super.y) => compil error
}}
[ March 28, 2006: Message edited by: Arno Reper ]
 
Djoga Djogov
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!
Now, after your answers it looks obvious to me (and also the question looks stupid), but the topic in the book was going on so much about the redefining(hiding) that I got confused what the subclass really knows about its superclass' static members.
Thanks again.
 
BWA HA HA HA HA HA HA! Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic