Mustafa
Mustafa

Reputation: 177

Is it better to initiate a new variable every time or call one from another class in java?

I am programming a game for android in framework called libgdx(if that is of any importance).

So I have about 8 different classes and in each one I need to measure things relatively to screen size so I make variables width and height.

My question is:

Is it better (for performance) to initiate new width and height variables in every class or just make them in one class and call them from other classes every time I need them??

Upvotes: 0

Views: 74

Answers (2)

Zane Sterling
Zane Sterling

Reputation: 138

It would be more efficient to use a single pair of height and width variables and access them elsewhere, as you're using less memory.

Upvotes: 1

e.doroskevic
e.doroskevic

Reputation: 2167

You could implement an abstract class and declare these variables accordingly. Then using a getter you could retrieve these values in the other class.

By employing an abstract class you would be able to maintain some sort of a default hight and width and override it whenever required.

Upvotes: 1

Related Questions