This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hello, 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.
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.
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.