What does the 'Static' keyword do?

I have a program where there are two shapes Player1 and Player2, both inherited by the class Player.

Public class Player{

    public int xPos;
    public int yPos;

    // more code


The player1 class:

Public class Player1 extends Player{

    public Player(){
        xPos = 200;
        yPos = 200;
    // more code

The Player2 class:

Public class Player2 extends Player{

    public Player2(){

        xPos = 400;
        yPos = 200;
// more code

In this case, should I use static for the xPos and yPos in the Player class?


If xPos and yPos were static, every instance of a Player would have the same position.

No. Static variables are members of the class, you want your position variables to be members of the object (non-static).

The static keyword in front of a variable signifies that the variable in question belongs to the "class itself" as opposed to individual instances. In other words changing it with one instance changes it with all of them. So in this case, you should not do what you are asking.


static as a keyword in Java means that the field is a Class field not a instance field. These are supposed to be used for fields that are used between instances of the class and don't have any meaning for a given instance. In your case the xPos and yPos are for each instance of the class.

Take a look at this static tutorial

The short answer is no.

A static variable or method means that no instance of this object is required to make use of it (for example, we don't create a new instance of class System when we do System.out.println). It also means that the state of the object is consistent every time we call it; that is to say, if we set some value inside of a class with a static field, that value will stay consistent throughout all instances of that class.

Every x and y position is unique with respect to each instance of your Player object. You don't want to use static.

You use static when you want the values to be same in all its references. In your case i guess you want the xPos and yPos to be different for different players. In other words for each object you create for the class Player you want xPos and yPos to be different. So you don't declare them as static.

Additional Info: If you declare some variable as static, the method that uses it should be declared as static too.

No. Don't use static attributes in this case because xPos and yPos are attributes (properties) of each Player. Each Player that is created (instance) must have its own x and y.

The static keyword is used to create something that belongs to the class and not to the instance of the class.

For example, if xPos was static you could call Player.xPos to get the value of xPos, without the need to have an instance of the class, and all Players would have the same xPos and yPos.

Need Your Help

python-notify module & cron: gio.Error

python cron env

I'm asking some help to show notifications using python-crontab, because everything I've tried do not work. The display is not initilised when the script is launched by cron. When I start it manual...

What is the command to delete empty lines in a file using sed?

linux unix sed command

What is the sed command to delete empty lines in a file. What is the command (sed command?) to delete empty files in a folder?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.