File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes doubt about inheriting static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "doubt about inheriting static methods" Watch "doubt about inheriting static methods" New topic

doubt about inheriting static methods

Mala Sharma
Ranch Hand

Joined: Oct 29, 2004
Posts: 76
Suppose i have 2 classes, A(parent class) and B (child class). A has a static method C. if i want to call that static method from the public static void main(string args[]) in class B. Would i do somethig like this in class B.

because the static method belongs to the entire class. So, im thinking child class can just call that method from the parent class without instantiating it first?. I hope i haven't conufesed anyone with my wording. THanks a lot.

Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
You can just do "C();" and it will work.

Mike Gershman
SCJP 1.4, SCWCD in process
Mala Sharma
Ranch Hand

Joined: Oct 29, 2004
Posts: 76
Oh, so i can call static method from the parent class in the child class just by its name?. I didn't know that. THanks a lot!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Static methods ARE inherited, so you can use just the name. However, you cannot override them, so they don't participate in polymorphism. Still I typically use the A.C() syntax to clarify that C() is a static method.


Java API Documentation
The Java Tutorial
Joel McNary

Joined: Aug 20, 2001
Posts: 1824

Just to clarify:

You can't override them, but you can shadow them. This means that which method is called is determined not a run-time, but at compile time. Consider the following (rather contrived) code:

This will produce the following output:

Note what happened with the last print -- it printed Hello, World. This is because the reference type of the variable is HelloWorld, so the compiler binds the method call to HelloWorld instead of the runtime type of BonjourMonde.

Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
I agree. Here's the link:
subject: doubt about inheriting static methods
It's not a secret anymore!