Detritus
Detritus

Reputation: 357

Java: Memory usage of the final keyword?

When you declare a final variable (constant) in a class, for example:

private static final int MyVar = 255;

How much memory will this require if I have 100,000 instances of the class which declared this?

Will it link the variable to the class and thus have 1*MyVar memory usage (disregarding internal pointers), or will it link to the instance of this variable and create 100,000*MyVar copies of this variable?

Unbelievably fast response! The consensus seems to be that if a variable is both static and final then it will require 1*MyVar. Thanks all!

Upvotes: 17

Views: 15341

Answers (12)

Vivek Verma
Vivek Verma

Reputation: 21

Static means, one instance per class, static variable created once and can be shared between different object.

Final variable, once value is initialized it can't be changed. Final static variable use to create constant (Immutable) and refer directly without using the object.

Upvotes: 1

Harsha
Harsha

Reputation: 49

The keyword "final" helps you to declare a constant with a specific amount of memory, where as the keyword "static" as its prefix will gives a single instance of this constant, what ever be the amount of memory consumed...!!!

Upvotes: 1

earcam
earcam

Reputation: 6682

The static declaration means it will only have one instance for that class and it's subclasses (unless they override MyVar).

An int is a 32-bit signed 2's complement integer primitive, so it takes 4 bytes to hold it, if your example wasn't using static you'd just multiply that by the number of instances you have (for your example of 100,000 instances that's 0.38 of a megabyte - for the field alone, extra overhead for actual classes).

The final modifier on a field means it cannot be repointed to another value (whereas final on a class of method means it cannot be overridden).

Upvotes: 4

axtavt
axtavt

Reputation: 242686

In addition to the fact that static fields belong to their classes, and thus there is only one instance of static varaible per class (and per classloader), it's important to understand that static final variables initialized by compile-time constant expressions are inlined into classes that use them.

JLS §13.1 The Form of a Binary:

References to fields that are constant variables (§4.12.4) are resolved at compile time to the constant value that is denoted. No reference to such a constant field should be present in the code in a binary file (except in the class or interface containing the constant field, which will have code to initialize it), and such constant fields must always appear to have been initialized; the default initial value for the type of such a field must never be observed.

So, in practice, the instance of static final variable that belong to its class is not the only instance of value of that variable - there are other instances of that value inlined into constant pools (or code) of classes that use the variable in question.

class Foo {
    public static final String S = "Hello, world!";
}

class Bar {
    public static void main(String[] args) {
        // No real access to class Foo here
        // String "Hello, world!" is inlined into the constant pool of class Bar
        String s = Foo.S; 

        System.out.println(s);
    }
}

In practice it means that if you change the value of Foo.S in class Foo, but don't recompile class Bar, class Bar will print the old value of Foo.S.

Upvotes: 2

Peter Lawrey
Peter Lawrey

Reputation: 533492

You will have one instance per class. If you have the class loaded more than once (in different class loaders) it will be loaded once per class loader which loads it.

BTW: Memory is surprising cheap these days. Even if there was a copy per instance, the time it takes you to ask the question is worth more than the memory you save. You should make it static final for clarity rather than performance. Clearer code is easier to maintain and is often more efficient as well.

Upvotes: 0

wesoly
wesoly

Reputation: 552

There will be only 1*MyVar memory usage because it is declared as static.

Upvotes: 12

tcooc
tcooc

Reputation: 21209

final makes is 1*instances memory usage.

However, static makes it simply 1.

Upvotes: 1

janhink
janhink

Reputation: 5023

The crucial part here is that you declared the variable as static because static variables are shared among all instances of the class, thus requiring only as much space as one instance of the variable. Declaring a variable final makes it immutable outside its declaration or constructor.

Upvotes: 1

DaveH
DaveH

Reputation: 7335

It's static, so there will be only one instance created, so however many bytes are required to hold one int primitive will be allocated

Upvotes: 0

Amir Rachum
Amir Rachum

Reputation: 79625

The final keyword is irrelevant to the amount of memory used, since it only means that you can't change the value of the variable.

However, since the variable is declared static, there will be only one such variable that belongs to the class and not to a specific instance.

Taken from here:

If a field is declared static, there exists exactly one incarnation of the field, no matter how many instances (possibly zero) of the class may eventually be created. A static field, sometimes called a class variable, is incarnated when the class is initialized . A field that is not declared static (sometimes called a non-static field) is called an instance variable. Whenever a new instance of a class is created, a new variable associated with that instance is created for every instance variable declared in that class or any of its superclasses.

Upvotes: 21

dantuch
dantuch

Reputation: 9283

static means you will have only one instatnce

final just means, that you can't reassign that value.

Upvotes: 1

Thomas
Thomas

Reputation: 88707

It's static and thus class scope -> 1.

Edit: actually, it depends on the class loaders. In the general case you have one copy of the class but if you have multiple class loaders/class repositories (might be the case in application servers etc.) you could end up with more.

Upvotes: 3

Related Questions