1
 Duck d = new Duck();
 string[] s = {};
 d.main();

Will the compiler generate an error as we are trying to call a static method using a reference variable instead of the class name?

Mia Clarke
8,2344 gold badges51 silver badges62 bronze badges
asked Mar 21, 2011 at 15:53
1
  • 1
    I never understand these questions. How hard is it to try it for yourself? That way you get a correct answer immediately. This way you may not get an answer at all, and you will probably get a few incorrect answers as well. Commented Mar 22, 2011 at 1:06

4 Answers 4

5

It is legal Java as defined by the JLS to call a static method via a reference. But it is frowned upon in many coding standard. Therefore some compilers and some IDEs support emitting warnings for it.

answered Mar 21, 2011 at 15:56
Sign up to request clarification or add additional context in comments.

Comments

3

If you use a standard compiler, it won't.

But it should.

You should never ever call a static method that way. There's absolutely no value whatsoever in doing so, it isn't quicker or more readable, but it's a ticking time bomb. Consider this scenario:

class A {
 static void bar() {
 System.out.println( "A" );
 }
}
class B extends A {
 static void bar() {
 System.out.println( "B" );
 }
}

Then somewhere in your code, you do this:

 A foo = new B();
 foo.bar();

Now, which bar() method is being called here?

answered Mar 21, 2011 at 15:57

1 Comment

It is quite an interesting scenario. I wonder what would be the outcome is.
1

It depends on the compiler settings. With eclipse default settings it will generate a warning, for example.

So try it with your compiler settings.

Generally, it does not generate an error (as defined by the JLS)

answered Mar 21, 2011 at 15:54

9 Comments

No, this question has a definitive answer. Compilers are allowed to differ on warnings but they all have to accept the same set of input programs, when they call themselves Java compiler.
@jmg - well, check the eclipse warning/errors configuration. You can set this to be an error. So if his compiler is configured differently, then it will generate an error.
@Bozho: I know, nevertheless it is clearly defined by the JLS and not by Eclipse what a valid Java program is.
For any given Java version there's only one Java standard and only one standard compiler configuration. Some tools will allow you to optionally restrict the constructs you accept even more, but one should never forget that these are all optional extras.
@jmg, @biziclop - I agree with that. But if his compiler is set differently then the answer will be incorrect in his context if it says "it won't generate error". I'll add a clarification about that.
|
0

First to your question,the answer is no.Obviously,you can use a reference variable instead of the class name to call a static method inside the class,but just because it's legal doesn't mean that it's good.Although it works,it makes for misleading and less-readable code.When you say d.main(),the compiler just automatically resolves it back to the real class.

answered Mar 3, 2021 at 13:45

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.