Why can I not have an abstract and static method in the same class in Java

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?


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.

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().)

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.

Category:java Time:2012-01-09 Views:0

Related post

  • PHPunit mockobject abstract and static method 2010-07-14

    I would like to test a method from an abstract class. In this class is there a abstract method with is static. I use PHPUnit. With normal abstract methods it works: <?php abstract class AbstractClass { public function concreteMethod() { return $th

  • Why can't static methods be abstract in Java 2008-12-16

    The question is in Java why can't I define an abstract static method? for example abstract class foo { abstract void bar( ); // <-- this is ok abstract static void bar2(); //<-- this isn't why? } --------------Solutions------------- Because "ab

  • Abstract classes and methods in Java, Inheritance 2009-01-06

    I have class B, which inherits from class A. The superclass A is abstract, containing one abstract method. I don't want to implement the abstract method in class B, therefore I need to declare class B as abstract as well. Declaring class B abstract,

  • C# virtual (or abstract) static methods 2009-04-18

    Static inheritance works just like instance inheritance. Except you are not allowed to make static methods virtual or abstract. class Program { static void Main(string[] args) { TestBase.TargetMethod(); TestChild.TargetMethod(); TestBase.Operation();

  • Static method , Abstract method , İnterface method comparision? 2010-04-15

    When i choose these methods? i can not decide which one i must prefer or when will i use one of them?which one give best performance? First Type Usage public abstract class _AccessorForSQL { public virtual bool Save(string sp, ListDictionary ld, Comm

  • "Abstract static" method - how? 2010-05-29

    There are already several SO questions on why there is not abstract static method/field as such, but I'm wondering about how one would go about implementing the following psuedo-code: class Animal { abstract static int getNumberOfLegs(); // not possi

  • php abstract classes and interfaces involving static methods? 2011-05-13

    I'm trying to work out how to best complete my design work on my classes. my situation. i have an order abstract class that contains order methods and information that are required for 2 children classes order_Outbound and order_inbound each child cl

  • Can sub-classes modify behavior of static methods in an abstract super-class in Java? 2011-06-23

    I'm using "Parameterized" runner to execute some tests. I am generating the test data from XML file. This works fine. Now I want to apply this behavior to multiple test classes without having to replicate the test data generation code. So I made an a

  • Static methods in an abstract class 2011-07-01

    I want to define an abstract method like so: public abstract class Saveable { public Set<Field> getFieldSet(); ... } getFields() should always return the same output regardless of the object's state. However, I can't declare this as a static me

  • Static method in abstract class to return IEnumerable 2011-07-12

    Is there a way of putting a static method in an abstract class that can return the derived type? Does a static method even know what type it is even being called from in C#? For example, a base class could be public abstract class MyBase { public sta

  • How can I get the current type from a static method in abstract class? 2011-07-19

    How can I get the current Type in a static method that is defined in an abstract class? Please note that as the method is defined in an abstract class, I can't use typeof. Why would I want to do this? A possible usage is attributes. Consider the foll

  • how to access overridden, non static method of abstract class in subclass 2011-12-02

    In my program there are only two classes one is Parent ,which is an abstract class and having a non static concrete method, void show(), now there is another class, Child which extends the abstract class, Parent and override the show() method. So now

  • Should I create static method or abstract superclass 2011-12-16

    I am trying to refactor a project in which there are same methods which are spread across various classes. To reduce code duplication, should I move the common code to an abstract superclass or should I put it in a static method in a utility class? E

  • Abstract class with static methods. Is that correct? 2012-01-01

    Task: I want to use some methods for many classes. Methods are same, so there is no need to implement them for each class. In my case - I work with android SDK and I send http request to server. Problem: There is idea to use construction like this: c

  • How to resolve abstract static method design issue? 2012-01-12

    I have several queues that all extend a Queue class. I want to do a Queue manger base class to handle adding to the queue (and other operations). If you take a look at the following code, I created a base class called QueueManger that has a static me

  • C# Method Attribute force requirements such as abstract or static? 2012-03-06

    This may be more of a design question then a "Can I do this?" question. I'm creating an attribute which its target is defined as Method. Is there any possible way to impose restrictions on the domain of methods,i.e that the target method must be decl

  • How to abstract a static classes 2012-03-07

    I have a static class. I can modify it and make it extends interface\abstract class. It contains lots of readonly and consts members. staic methods. In order to make this code testable, I want to Separate it to DTO and Manager. Abstract each of them.

  • Testing Abstract Class Concrete Methods 2008-09-19

    How would I design and organize tests for the concrete methods of an abstract class? Specifically in .NET. --------------Solutions------------- You have to create a subclass that implements the abstract methods (with empty methods), but none of the c

  • Mocking non-public static methods in abstract classes with JMockit? 2008-10-22

    I have the following class: public abstract class AbstractParent { static String method() { return "OriginalOutput"; } } I want to mock this method. I decide to use JMockit. So I create a mock class: public class MockParent { static String method() {

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.511 (s). 13 q(s)