Is a static variable in java internally processed?

I have a question about the static keyword. Say, for example, we have this piece of code.

public class Foo { private int age; private static int weight; .. ... } 

Speak basically, you create 2 objects. You change age in one, and then change weight in another. Does this mean that the weight also changes in the first object? If so, does that mean weight is a pointer?

I think my question in a nutshell will be. How does static work inside? Is this essentially a pointer type?

+4
source share
3 answers

Static variables are shared by all instances of the class. These are not pointers, but all references (static / instances) point to the same memory block containing the static value. It is also important to note that static variables are associated with a class, and not with any object, which means that static variables can be initialized and used without creating an object.

+2
source

To understand how static works, you need to know about two possible members:

  • Class members . Together with each instance of this class, this type of member is defined using the static . They belong to a class, not a specific instance.
  • Members of the instance . Members declared in a class that are not static belong to the instance. Each of them will have a specific value defined for it (or not, in the case of a null object).

To answer your specific question, each instance of a class shares class members with others, referring to fixed locations in memory. This exchange implies that changing the value of a class member in this instance updates the value for all of them.

+2
source

Java masks pointers used by basic memory management, so you usually don't need to worry about them. But let's align some terms.

Java class is a plan; it defines behavior (methods) and state (variables). Some parts of the object, in particular those that are static, share all instances of this class (working in the same runtime) and do not even require that the class to be created be accessible to everyone.

An object is a special instance of a class. All non-static variables and methods are unique to this object and only this object. When you use a new one, you instantiate a new class object.

This becomes apparent when you consider how static methods execute differently than non-static methods:

  MyObject.aStaticMethod();//Note I don't instantiate the class. MyObject obj = new MyObject(); obj.aNonStaticMethod(); //For non-static methods, I MUST have an object instance of the class. 

Note in the above example, the obj token is a link (which is different from a pointer) to a specific place in memory. This memory location is an instance of obj and includes references to code variables and states that are stored in another memory location allocated for the MyObject class. In this second area, all static and static variables are saved. But usually you don’t need to worry about this level of subtle details.

+2
source

Source: https://habr.com/ru/post/1502091/


All Articles