• 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 classes

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey..

1) How can I name a static method in a class? I can creat a method with only the "static" word, but i cant name that method. it gives an compiler error. Is it illegal to name a static method?

2)Suppose I have a "base" class and the "sub" class which is extended from "base" class. If both these classes have main() methods, when I creat an instance of "sub" class, why doesnt it run the static main() method in "base" class. But all the other static methods in the "base" class run when an "sub" class instance is created.

Thank you guys...
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sounds like you are doing so weird things with static methods. Static methods are not inherited and should not be called from an instance of a class, but rather from the class itself.

example:


Could you post your code? and use the code tags (you'll see a code button below where you post)
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) A static method can look like this... (from Head First Java)


2) Think "overriding methods."

[ May 27, 2005: Message edited by: Alan Jump ]

(edited to remove oversize quote)
[ May 27, 2005: Message edited by: Alan Jump ]
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alan Jump:

2) Think "overriding methods."



No, static methods cannot be overriden. They can be 'hidden' by a static method with the same signature in a subclass, but they are never overriden.
 
Alan Jump
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bad...you're right...if there's a main() in the superclass and also in the subclass, the fact that the "standard" signature for main() includes the static designator will hide the main() in the superclass from the main() in the subclass. My thinking was that since neither the arguments or return type change between them, it could be thought of as an override.

Hrm...I'd better stop before I confuse anyone (including myself) any worse...
 
reply
    Bookmark Topic Watch Topic
  • New Topic