-2

BEHOLD CODE

public class Main {
    static int bob;
    //create a foo
    static Foo foo;
    //cuz why not have a second foo
    static Foo foo2; 
    public static void main (String args){
        foo = new Foo();
        foo2 = new Foo();
        //creating a local variable with foo2's value +5
        bob = foo2.anIntThatWillNeverChageValue+5;
    }
}

Foo's class is very simple

public class Foo {
    static int anIntThatWillNeverChageValue =5;

    public Foo(){

    }
}

So you might be thinking "this code compiles fine, whats the problem?". Well java says at the line:

bob = foo2.anIntThatWillNeverChageValue + 5;

in the main class that

The static field Foo.anIntThatWillNeverChageValue should be accessed in a static way

Now I do understand this is generally good practice, and simply accessing Foo's static class by saying

bob = Foo.anIntThatWillNeverChageValue + 5;

would be better practice, more efficient, and simpler. however what about in a polymorphic environment when foo is extended by 3 classes . I wouldn't want to just access the basic Foo's variable, I would want my particular object's variable. Of course the obvious solution to this would be simply making the variable non-static, but in that case you are creating a new variable every time you create a new child of Foo. wouldn't it be more efficient to just access it staticly?

Edit:

quoting from Why should the static field be accessed in a static way?

To address the question of why: In Java, when you declare something as static, you are saying that it is a member of the class, not the object (hence why there is only one).

is an answer that is essentially what Im asking. Im trying to use a static method to avoid UNECISARY copies of a non static field. My question is asking why it is against Java conventions to access a static method polymorphically, and also was intended to ask for possible solutions that would be conventionally acceptable and/or more efficient.

Community
  • 1
  • 1
idiotprogrammer
  • 146
  • 2
  • 12
  • 1
    Static variables are resolved to the compile-time type. And if a value will never change, make it final. – chrylis -cautiouslyoptimistic- Sep 21 '15 at 22:44
  • 1
    possible duplicate of [Why should the static field be accessed in a static way?](http://stackoverflow.com/questions/5642834/why-should-the-static-field-be-accessed-in-a-static-way) – Mick Mnemonic Sep 21 '15 at 22:47
  • 1
    @MickMnemonic Question's a duplicate, but those answers aren't very good. – chrylis -cautiouslyoptimistic- Sep 21 '15 at 22:59
  • 1
    @chrylis, I don't think there's anything wrong with those answers. However, reading through this question more closely, it might be that OP does not fully understand how `static` variables work, and because of that, the answers to the other question may not be relevant. Related: [Why are static variables considered evil?](http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil) – Mick Mnemonic Sep 21 '15 at 23:09
  • a better question is why did java allow it :) – ZhongYu Sep 21 '15 at 23:34

1 Answers1

1

If you want the variable to be accessible by only subclasses of Foo, then you should declare it protected.

If it is to be used only by the class it is declared in, then make it private.

Static variables are meant to be accessed without the need for an instantiated type. If you want to access it by using an instantiated type of the class, then you should not be declaring the variable static

smac89
  • 39,374
  • 15
  • 132
  • 179