I am a beginner in Java, this sorry, if the question is stupid.
Here's the thing — we write a server for online games, but it does not matter.
Suppose you have a class "Player". The objects of this class stored all the characteristics of the players — health, strength, agility, etc:
public class Player extends Object {
public int hp = 0;
public int str = 0;
public int dex = 0;
public int agil = 0;
// ... etc.
}
Accordingly, such facilities required by the number as much as the players.
Let's say we have 50 million players. Such memory objects will eat decently. For this, I want thoroughly to understand the amount of memory the Java.
Accordingly, the question is — if I need to create some methods that operates with the player — do I have to make them static?
Why I ask — I thought if I create a not-static method in class Player, then whenever you create an object of that class in the memory will be allocated a place under the code of all its methods. Ie if I want method savePlayer(), creating 50 thousand players, I will get in the memory 50 thousand copies of the code of this method. That is clearly a fail. And in this I have to create a static method of a type savePlayer(Player player).
Am I right?
Just the fact that I decided to check it out. Looked in the Profiler, the Eclipse... Now I have one instance of the Player is 112 bytes. After that, I didn't add static method in the "player" has started again... and it is strange — court player still occupies 112 bytes. I was expecting to see an increase in the size of the instance "player" on the size of the code added to the method.
Explain pliz.