2

I have the following class:

public abstract class A { public abstract String doSomething(String X, String Y); public static String doSomething(String X, String Y){return X + Y;} ... } 

The issue I have is that the static and abstract doSomething() methods seem to clash as duplicates. I thought this should be fine because the static method belongs to the class, not an instance of the class, so I was going to use the abstract method to enforce the method on all subclasses and the static method as a helper so that I have nicely factored code.

I know I could probably add an interface into the mix, but I don't really understand what's wrong with my abstract and static methods existing on the same class. What's wrong with this?

3 Answers 3

8

In Java it is valid (despite being misleading and confusing) to call a static method from an object instance rather than the class name (despite warnings generated by many compilers).

System.out.println(String.valueOf(true)); // Prints "true". System.out.println("".valueOf(true)); // Prints "true", unfortunately. 

So the following seemingly valid code wouldn't know which of those methods to call:

A a = getInstanceOfConcreteSubclassOfA(); a.doSomething(null, null); // Compiler can't decide which method to call... 

Unfortunately, it's just one of the few ugly corners of the Java language.

Sign up to request clarification or add additional context in comments.

5 Comments

To clarify slightly: it's the compiler, not the runtime, that would have to figure out which method to call.
You can't override static methods. Can you?
@srikanthradix: nope, static methods cannot be overridden.
Personally I wouldn't call it ugly. The joy of Java is that it blocks you from doing things which are misleading. This is a great example. If you want a "helper" method, then it is better to give it a different name so that someone reading your code sees the difference at a glance between the method itself and its helper.
@couling: It's ugly because it is misleading. a.staticMethod() looks like it's calling a non-static virtual method, but it's actually calling a static non-virtual method. And ((A)null).staticMethod() looks like it should throw a NullPointerException, but it doesn't.
3

It's not specific to abstract methods; in general, Java doesn't let you have two methods with the same parameter-types but one being static and one not. Something like this:

public String doSomething(String X, String Y){return X + Y;} public static String doSomething(String X, String Y){return X + Y;} 

would also be illegal.

(This makes sense when you consider that you're allowed to call a static method "on" an actual instance, or for that matter, on any expression of the appropriate type. The compiler translates ((A)null).staticMethod() to A.staticMethod().)

Comments

1

Each method has a signature composed of:

method name parameter type Return type 

If 2 methods have the same signature, this will cause an error.

the word static does not interfere in the signature of the method just like const.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.