• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static classes

 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • 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...
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • 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)
 
Alan Jump
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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...
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic