Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If I call a static method, does the constructor run

I have a class called Tile with a static method public static BufferedImage grass() and inside the class is also a constructor public Tile() which sets a variable.

So what I'm wondering is if I call the grass() method from another class Tile.grass(), will the constructor run?

I'm assuming not as my grass() method is returning null.

Here is my code:

import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Tile {

    public static final int size = 50;

    private static BufferedImage mapSprites;

    public Tile(){
        try{
            Tile.setMapSprites(ImageIO.read(getClass().getResource("res/mas.png")));
        } catch (IOException e){
            e.printStackTrace();
        }
    }

    public static BufferedImage grass(){
        return getMapSprites().getSubimage(0, 0, 10, 10);;
    }

    private static BufferedImage getMapSprites() {
        return mapSprites;
    }

    private static void setMapSprites(BufferedImage mapSprites) {
        Tile.mapSprites = mapSprites;
    }
}

I could create a function the gets the mapSprite for every tile I return. But I don't think that would be very efficient. Is there another solution?

(Just for reference here is how I am calling it in my Map class)

public void render(){
    g.drawImage(Tile.grass(), 0, 0, null);
}
like image 881
Dominic Sore Avatar asked May 18 '15 16:05

Dominic Sore


People also ask

Does static require constructor?

Static classes cannot contain an instance constructor. However, they can contain a static constructor. Non-static classes should also define a static constructor if the class contains static members that require non-trivial initialization.

What happens if a method is declared as static?

If a method is declared as static, it is a member of a class rather than belonging to the object of the class. It can be called without creating an object of the class. A static method also has the power to access static data members of the class.

Can a static methods call a non static constructor?

You cannot call static methods using an object of the non-static class. The static methods can only call other static methods and access static members. You cannot access non-static members of the class in the static methods.

Can we use constructor for static method in Java?

No, we cannot define a static constructor in Java, If we are trying to define a constructor with the static keyword a compile-time error will occur. In general, static means class level.


1 Answers

No, the constructor doesn't run if you only call a static method of a class. There is no instance of a class associated with a call to a static method. That is why mapSprites is null.

To populate mapSprites, you can move the code that initializes it out of the constructor and into a static initializer. This way, it will run the first time the class is referenced, so that mapSprites is initialized properly when you call a static method for the first time.

static {
    try{
        Tile.setMapSprites(ImageIO.read(Tile.class.getResource("res/mas.png")));
    } catch (IOException e){
        e.printStackTrace();
    }
}

With static initializers, you must be careful to avoid having any exception propagate out of it. If it does, then it will be wrapped in an ExceptionInInitializerError that will be throw and that will be bad news for your program.

You may also want to make the (now useless) constructor private, to prevent instantiation of the class, because your class is now a utility class, where everything is static, and an instance of this class is now useless.

like image 99
rgettman Avatar answered Oct 27 '22 04:10

rgettman