Reputation: 1645
I have two questions concerning objects and the use of static in Java. I found I could best explain my questions if I asked them with the same background:
I have one main class in my program. First it initializes a lot of things and then it starts the run method which cycles through a while (true) loop.
I also have a lot of other classes such as let's say a circle which I create during the initialization in my main class and save in a local variable along the lines of myCircle = new Circle();
.
I have also made some classes of which all variables and methods are static.
I've noticed that I don't have to create an object of such a class during initialization because everything in it is static. Whenever I need something from one of those classes, I can just call it like StaticClass.someMethod()
or StaticClass.someVariable
.
This in contrast to the circle where I go myCircle.anotherMethod()
. Had I made that method static I could have used Circle.anotherMethod()
.
Now my question is: Why create a new object and save it if I can just make everything in that class static (assuming that I'll only ever need ONE such object)?
More importantly: One of my static classes has a ton of constants which are images that it reads from a file. Since I haven't made a constructor, I don't know when it is actually loading the images. There are several different occasions when I would call StaticClass.someImage
and I'm wondering if it is now loading the images from a file multiple times.
So my second question is: When does Java load all the variables of a static class which doesn't have a constructor? (in other words, when does it create that object?)
Upvotes: 1
Views: 123
Reputation: 96385
1st question: Generally I think it's better style to create an object rather than make everything static because you are not hard-coding the scope of the fields using static. If you ever change your mind about whether you need multiple objects, or if you want to extend it with subclasses, you will have a lot more work to do to make the change, because you originally designed the class with the assumption of that fixed scope in mind. (This is a judgment call, it depends on the situation.)
2nd question: All classes have a constructor. If you don't specify one you get a default one made for you. The static variables of a class get initialized when the class is loaded. Once a class is loaded the classloader doesn't load it again, it goes with what it found the first time. When you have a web application where there are dependencies on multiple versions of a jar, which version gets loaded depends on which was referenced first.
Upvotes: 1
Reputation: 2510
This seems a design choice between
In your situation, both may work. Although answers should not be opinion-based, I'd go slightly in the favor of the first choice (the same as your own), because of the exact same argument you provided: a lot of static constants. However, this will bring your code to a more C-style, procedural form.
If, on the other hand, you don't make heavy use of static fields and you won't use your class in the same way you use Arrays
, for example, then the second choice is better; the reason being flexibility (polymorphism is one example that gives flexibility).
As for your second question, the static code gets executed when the class is loaded. For example:
class MyClass {
static final int SPEED_OF_LIGHT = 299792458;
static {
foo();
}
static void foo () {
// ...
}
}
The method foo
above gets executed at loading time and needs no objects to do its thing (of course, 'cause it's static). Also, static varaibles are initialized at class loading time as well.
Upvotes: 1