Francisco Romero
Francisco Romero

Reputation: 13199

How does the static keyword work in Java?

I'm reading Java tutorials from the begining and I have a question about static keyword on fields or variables. As Java said here:

Class Variables (Static Fields) A class variable is any field declared with the static modifier; this tells the compiler that there is exactly one copy of this variable in existence, regardless of how many times the class has been instantiated. A field defining the number of gears for a particular kind of bicycle could be marked as static since conceptually the same number of gears will apply to all instances.

With that, I guess that if you have an object (in this case, an instance of the class Bicycle) and a field inside of it that its static then, independently of if you are refearing to bicycle1 or bicycle2, the field that its static will have the same value. Am I wrong or I understand it well?

I mean, if I have:

Bicycle bicycle1 = new Bicycle();
Bicycle bicycle2 = new Bicycle();

and in the class Bicycle I have a static field like:

class Bicycle{
   static int gears;

   //Methods to set and get gears
}

And in the bicycle1 I set the value of gears to seven:

bicycle1.setGears(7);

then if I try to get the value of gears in bicycle2 I should get the same value as I set on bicycle1, right?

System.out.println(bicycle2.getGears()); //7

Well, here is where I have the doubt because as Java said in the quote that I put above:

this tells the compiler that there is exactly one copy of this variable in existence

Where is this copy stored? How the objects access to that copy? When is this copy created?

Upvotes: 8

Views: 4308

Answers (4)

m0bius
m0bius

Reputation: 151

Where is this copy stored?

The copy (static variable) is stored in the Permanent Generation section, but if you use Java8 the Permanent Generation section no longer exists. The static variables and static methods are part of the reflection data which are class-related data and not instance-related.

How do the objects access that copy?

Every instance of class (object) that you have created has a reference to the class.

When is this copy created?

It is created at runtime when the class is loaded: this is done by the classloader of the JVM when the class is first referenced.

Static variables belong to the class, and not to instances of the class. Your intuition is right - you have only one copy regardless of how many object you create.

You can access a static variable using the name of the class, like in this example:

class Static {

    static int staticField;

}

public class UseStatic {

    public static void main(String[] args) {

        System.out.println(Static.staticField);

    }
}

The static fields are useful to create some kind of class constants.

Finally, to easily initialize a static field of a specific class you can use Static Initialization Blocks.

Sources: University course on java, java official documentation

Upvotes: 10

wribeiro
wribeiro

Reputation: 36

static variables in java are stored in the class, you don't need to create a instance of it to access them.

class Bicycle{
   public static int gears = 7;

   //Methods to set and get gears
}

You can access the static method like this

Bicycle.gears;

So, there's just one Bicycle class declared on java, when you instantiate a class it's create one instance of bicycle with all static attributes declared.

Upvotes: 1

scottb
scottb

Reputation: 10084

With that, I guess that if you have an object (in this case, an instance of the class Bicycle) and a field inside of it that its static then, independently of if you are refearing to bicycle1 or bicycle2, the field that its static will have the same value. Am I wrong or I understand it well?

When you instantiate a class in Java for the first time, the JVM creates two things:

  • an instance. A set of non-static fields is allocated onto the heap for each of the instances that you create. These instance fields are separate from all other instances (and are used to represent their object's state).

  • a Class object. Every class in Java has one, and only one, Class object ... no matter how many instances of it that are created. For example, the Class object for class String is Class<String> (which is expressed as a literal as String.class). You can think of the static fields of a class as belonging to the Class object. The lifecycle of Class objects is independent of the lifecycle of class instances; Class objects exist for as long as the JVM process is running (therefore, their static fields also exist that long).

Since a class has only one Class object, and since all instances of a class share that same Class object, the static fields of a class are shared by all the class instances that exist.

In general, it is useful to think of the static keyword as meaning "independent of any instance":

  • a static field belongs to the Class object and not to any instance
  • a static method is invoked through the Class object and has no direct access to any instance
  • a static member class instance is not dependent on any other instance

Upvotes: 3

user996142
user996142

Reputation: 2883

Where is this copy stored?

Static variables are stored in some static storage (in permgen, I believe), you should not bother about it.

When is this copy created?

They are created when class is accessed first time (loaded by class loader) and never deleted (unless class is unloaded)

How the objects access to that copy?

Instance has reference to its class, and class has reverence to all its variables. How exactly C structures are laid in memory is implementation-specific detail.

Since static vars are bound to class, not instance, you do not even need to instantiate class to access them. MyClass.myStaticVar is ok.

Upvotes: 0

Related Questions