Why can't we call static methods using the object in Java?
-
In the following code, why can't I call methods using the object sc. public class Basics22_Inheritance { public static void main(String[] args){ SubClass sc = new SubClass(); sc.subClassMethod(); //Why this shows me a warning? I am still //calling a static method from another static method sc.superClassMethod(); SubClass.subClassMethod(); //Accessing using the static way, i.e, //className.method(); SuperClass.superClassMethod(); //This won't work if we remove //static keyword for the method } } class SuperClass{ public static void superClassMethod(){ System.out.println("this is superClassmethod"); } } class SubClass extends SuperClass{ public static void subClassMethod(){ System.out.println("this is subClassmethod"); } }
-
Answer:
I have read but haven't confirmed by coding it up. You can access static members through any of the instances of a class but it is considered bad practice. Using the class name makes it clear that the member belongs to the whole class of objects rather than to a single instance.
Narendra Joshi at Quora Visit the source
Other answers
Yes we can call static methods using object references. We can call them using object reference as well as class name.
Vivek Vermani
static method are supposed to bind to the class, and you can directly use the class name to call them. It is not an error to call static methods via object references, but then it is not entirely useful. When you call a method via the object reference, it is generally meant that the method is going to manipulate on the state of that object and not affect any other objects. But static methods are shared among all the objects, and whatever manipulation you do in those methods are visible across all objects. So basically, its a conflict of concept. That is why Java warns you of calling static methods with object. You can just call the method with the class name only.
Sohom Majumdar
The reason why this generates a warning has to do with expectations on which method will be called in the case where the method is overridden in a subclass. A programmer might expect that, when you call a static method via an object, that it will call the static method implemented in the actual runtime class of the object (which might be a subclass of the declared class). This is what happens in some languages, but not in Java. In Java, static method calls are fully resolved at compilation time, which means that the actual runtime class of the object cannot be known. The compiler will call the method implementation defined in the declared class of the object being used to call the static method. Because this behavior may not be what the programmer expects, the compiler generates a warning.
Kelly Martin
You can call static methods using objects. Class Example{ static void printHello(){ System.out.print("Hello"); } public static void main(String[] a){ Example e = new Example(); e.printHello(); } } In the above code the statement e.print() on line #7 will be represented as Example.print() in ByteCode. Java knows which method is static. Actually this is a bad coding style and not an error. You are getting a warning because you are using an IDE which is programmed to show a warning when such kind of things are practiced.
Manoj Jawalkar
Related Q & A:
- Why can't I call on windows live messenger?Best solution by Yahoo! Answers
- Why won't video call work but webcam does?Best solution by community.skype.com
- Why can't my computer can't detect my external hard drive?Best solution by Yahoo! Answers
- Why isn't my Call of duty working?Best solution by Yahoo! Answers
- Why can't I send emails using Microsoft Outlook?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.