-1

Consider this example (warning-very bad code):

public abstract class A {

    static float foo;

    public static void loadfoo(float incomingfoo) {
        foo = incomingfoo;
    }
    public static void displayfoo() {
        System.out.println("your foo is" +foo);
    }

}

Class B extends Class A

public class B extends A {

    static float foo;

    //@Override (overide is not allowed for static methods.  dis is a problem...)
    public static void loadfoo(float incomingfoo){
        foo = incomingfoo;
    }
}

Class C is pretty much the same as B

public class C extends A {
    static float foo;

    //@Override 
    public static void loadfoo(float incomingfoo) {
        //I would like  a different static variable loaded into this class using this method
        foo = incomingfoo;
    }
}

finally the main Class runs the thing

public class Main {

    public static void main(String whatever[]){
        B.loadfoo(5);
        C.loadfoo(8);
        B.displayfoo();
        C.displayfoo();
    }
}

so the output of this is :

your foo is0.0
your foo is0.0

and I am aware this is because the displayfoo class reference the static foo in Class A, so please disregard this. I assume I have now been specific enough about describing my problem and goal. solutions anyone?

Edit: I feel like an idiot I completely forgot to actually state what I wanted to accomplish, but really all I want is for B and C to have there own static variables loaded into them without altering A's variable, which should be the default.

idiotprogrammer
  • 146
  • 2
  • 12
  • 3
    I don't see a question here? Could you be more specific as to what you are trying to do? – Tunaki Aug 30 '15 at 19:15
  • If you have not seen it already, I suggest that you review [the following](http://stackoverflow.com/questions/10291949/are-static-methods-inherited-in-java) post. – PM 77-1 Aug 30 '15 at 19:23
  • No, you have been specific enough about describing a well-known situation wherein static methods are not subject to polymorphism. But what is your problem? What do you want to achieve? What is your goal? – RealSkeptic Aug 30 '15 at 19:23
  • Have you considered using _singleton_ instances instead of calling static methods? – Solomon Slow Aug 30 '15 at 19:43
  • 1
    It seems that first you want to load the values using `loadfoo` to `foo` and then display the value of that `foo` using the `displayfoo` method. Well, I don't think there is anyway to do it using static methods. But You can do the same by making `displayfoo()` method abstract and overriding the same in the subclasses `B` and `C`. – Hardik Modha Aug 30 '15 at 20:00
  • `static` methods almost always sucks. Throw it away and use proper OOP. – Tom Aug 30 '15 at 20:51

3 Answers3

4

It looks like you need static access to two stateful objects with the same structure. In this case, an enum might be a solution:

public enum A {
  B, C;

  private float foo;
  // getter and (optional) setter for foo here

  public void displayFoo() { System.out.println("This foo  is " + foo); }
}

This way you can still access your object statically, but don't need to duplicate anything else:

A.B.setFoo(5);
A.C.setFoo(8);
A.B.displayFoo(); // 5
A.C.displayFoo(); // 8

If you then need a static default, I would make it a method on A:

enum A {
  A getDefault() { return A.B; }
}

A.getDefault().displayFoo();
Jorn
  • 20,612
  • 18
  • 79
  • 126
  • 1
    very creative Idea. It's interesting you say this as this was my original idea, but then I scrapped it because I am not very familiar with Enums, however I do believe this is a very valid solution, and I will quite likely end up using this. – idiotprogrammer Aug 30 '15 at 20:54
1

It seems that first you want to load the values using loadfoo to foo and then display the value of that foo using the displayfoo method. Well, I don't think there is anyway to do it using static methods.You can do this by making displayfoo() method abstract and overriding the same in the subclasses B and C.

Here is the code:

abstract class A {
     float foo;
     public void loadfoo(float incomingfoo){
        foo = incomingfoo;
     }
     public abstract void displayfoo();
}

class B extends A{
    @Override 
    public void loadfoo(float incomingfoo){
        foo = incomingfoo;
    }
    @Override
    public void displayfoo(){
        System.out.println("foo is " + foo);
    }
}

class C extends A{
    @Override 
    public void loadfoo(float incomingfoo){
        this.foo = incomingfoo;
    }
    @Override
    public void displayfoo(){
        System.out.println("foo is " + foo);
    }
}

public class Main {
    public static void main(String whatever[]){
         B b = new B();
         C c = new C();
         b.loadfoo(5);
         c.loadfoo(5);
         b.displayfoo();
         c.displayfoo();
    }
}

You can also check the same kind of question here.

Community
  • 1
  • 1
Hardik Modha
  • 12,098
  • 3
  • 36
  • 40
0

Static methods should be used by static method access and not by object instance. It's not supposed to be virtual because it's not belong to the object.

  • If you call B.loadfoo() then a method of B class is called.
  • If you call C.loadfoo() then a method of C class is called.
  • You cannot call a static method if it doesn't exist in the class.

There's no point to use static methods if you want to use polimorphism.