permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static classes" Watch "Static classes" New topic
Author

Static classes

Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
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

Joined: Dec 29, 2004
Posts: 1071
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

Joined: May 25, 2005
Posts: 26
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

Joined: Dec 29, 2004
Posts: 1071
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

Joined: May 25, 2005
Posts: 26
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static classes
 
Similar Threads
Calling overridden methods from a constructor
final being overridden??
Questions on inner class & cast down
Casting urgently! Marcus Green Exam1
does static methods inherit ? what about private ?