kampfgnu
kampfgnu

Reputation: 107

java public static final object

following code, containing file is here

public abstract class Quart extends TweenEquation {
    public static final Quart IN = new Quart() {
        @Override
        public final float compute(float t) {
        return t*t*t*t;
    }
    ...

if i call Quart.IN.compute(0.5f) somewhere in my running application (e.g. in a render() function that is called 60 times per second), does this create a new Quart on every call, or is it just allocated once?

it would make sense, right?

thanks, cheers

Upvotes: 3

Views: 6566

Answers (4)

Elyor Murodov
Elyor Murodov

Reputation: 1028

You can also check it's created only once by writing output when an instance of Quart is created:

public abstract class Quart extends TweenEquation {
    public static final Quart IN = new Quart() {

        { System.out.println("created"); }

        @Override
        public final float compute(float t) {
            return t*t*t*t;
        }
    ...

Upvotes: 0

nils
nils

Reputation: 1382

Just once. The static field IN is initialized during the first access to the class Quart.

Upvotes: 0

Marcel Stör
Marcel Stör

Reputation: 23535

is it just allocated once

Yes, you're calling the compute method always on the same single object.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691715

By definition, a final variable can only be assigned once. And static fields of a class are initialized when the class is loaded. So obviously, the IN Quart instance is created just once.

Upvotes: 4

Related Questions