1

Suppose I have an abstract class Game:

public abstract class Game { public static final int FRAME_LENGTH = 40; public static int getFrameLength() { return Game.FRAME_LENGTH; //What do I have to replace Game with? } } 

And there is the class App which extends Game:

public class App extends Game { private static final int FRAME_LENGTH = 50; } 

Now, when the method App.main() is executed, it prints 40, I would have expected 50, as App has overwritten the value of FRAME_LENGTH. I guess I would have to use something like self in the Game class, except java has no self.

How am I supposed to access the static property App.FRAME_LENGTH without knowing beforehand that the child class will be called App?

Edit

Okay, based on the first answer, I think I have to specify my question a little.

First, I change the scope of FRAME_LENGTH to public. For the sake of this question, this makes things easier. Second, I changed the method getFrameLength which was previously called main.

The problem I am facing: Right now, I do not know which classes will extend Game, and Game should not need to know, either.

If you look at the code, you will be able to see that Game.FRAME_LENGTH is 40 and App.FRAME_LENGTH is 50. Game.getFrameLength() returns 40 as expected, but App.getFrameLength() returns 50, I would expect (or rather want) it to return 50.

My question now is very simple: In the above code, what do I have to change return Game.FRAME_LENGTH; to, so it returns the correct value (meaning the value of FRAME_LENGTH of the class the method is called on), similar to self::$frameLength in PHP.

1
  • Override getFrameLength. Commented Jul 12, 2014 at 18:17

1 Answer 1

2

You can't access the App.FRAME_LENGTH varible from the main method, because it's defined as a private to the App class.

Also, you are not overriding the variable value (by extending the class). The class inheritance has nothing to do with static members


Update:

Seems like you don't need the static modifier at all. What you have to do is the following:

public abstract class Game { private final int FRAME_LENGTH = 40; public int getFrameLength() { return this.getValue(); //<-- Note the usage of the "this" keyword. } // Every subclass of Game must implement this method // and return their specific FRAME_LENGTH value public abstract int getValue(); } 

By using the this keyword, we're actually invoking the current instace (which can be a subclass) of the Game class.

Now, an example subclass of the Game can provide a different value of FRAME_LENGTH.

public class App extends Game { private final int FRAME_LENGTH = 50; @Override public int getValue() { return FRAME_LENGTH; } } 

Now, when invoking new App().getFrameLength() you will actually return the result of App#getValue() implementation and thus will receive 50. For example:

public static void main(String[] agrs) { System.out.println(new App().getFrameLength()); } 
Sign up to request clarification or add additional context in comments.

5 Comments

Wait a minute. If FRAME_LENGTH was public, I could (from anywhere) access App.FRAME_LENGTH and get 50, right? Now, when I run the method App.main, it needs to access that value. But if there were another class, say Example extending Game, when I run Example.main, the value in Example.FRAME_LENGTH should be used. How do I define that in Game.main?
When you type SomeClass.FRAME_LENGTH the compiler will try to access the static member of SomeClass called FRAME_LENGTH. Depending on the type of the accessor (public, private, etc.) it will be accessible or not. If you want to get the App.FRAME_LENGTH from Game, you have to change the private modifier of the App's FRAME_LENGTH varible to more wider scope.
I overhauled my question a bit, I hope it is clearer now what exactly I am asking.
I actually need static in this case, but for some other reasons, which have nothing to do with the issue here. Is there really no way to access a static property of the current class without explicitly using the class name?
Well...no. static variables don't have relation to class instances. They are only bound to the class definition and are accessed via the ClassName.StaticVariable pattern.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.