Accessing a static variable through an object reference in Java

Why can we access a static variable through an object reference in Java, as in the code below?

public class Static { private static String x = "Static variable"; public String getX() { return this.x; // Case #1 } public static void main(String[] args) { Static member = new Static(); System.out.println(member.x); // Case #2 } } 
+8
source share
6 answers

Generally, public variables can be accessed by everyone, and private variables can only be accessed from the current instance of the class. In your example, you are allowed access to the variable x from the main method, because this method is in the Static class.

If you're wondering why you are allowed to access it from a different instance of the Static class than the one you are currently using (which is usually not allowed for private variables), this is simply because static variables do not exist based on each instance, but based on each class. This means that the same static variable A can be accessed from all instances of A.

If this is not the case, no one will be able to access the private static variable at all, since it does not belong to one instance, but all of them.

+10
source

It is not recommended to refer to a static variable in this way.

However, your question was, why is this allowed? I would suggest that the answer is that the developer can change the instance member (field or variable) to a static member without having to change all references to this element.

This is especially true in a multi-developer environment. Otherwise, your code may not compile just because your partner has changed some instance variables to static variables.

+3
source

The reason this is allowed is because JLS says it is. Specific sections that allow it are the JLS 6.5.6.2 (for cases member.x ) and the JLS 15.11.1 (in both cases). The latter says:

If the field is static:

  • If the field is a non-empty finite field, then the result is the value of the specified class variable in the class or interface, which is the type of the main expression.

  • If the field is not final or is empty final, and the field is accessed in the initializer of the class variable (Β§8.3.2) or the static initializer (Β§8.7), then the result is a variable, namely the specified class variable in the class, which is the type of the main expressions.


Why is this allowed by JLS?

Honestly, I do not know. I can't think of good reasons to let them.

In any case, using a link or this link to access a static variable is a bad idea, as most programmers can be misleading in the belief that you are using an instance field. This is a serious reason not to use this Java feature.

In your first and second cases, you should refer to the variable as x or Static.x and not member.x . (I prefer Static.x .)

+2
source

Static variables are also called class variables because they are available for every object of this class.

As a member, it is an object of the Static class, so you can access all static, like wll, as non-static variables of the Static class through a member object.

+1
source

The non-static member is a member of the instance. A static member (class wide) could not access instance members because there is no way to determine which instance belongs to certain non-stationary members.

An instance object can always refer to static members because it belongs to a class that is global (shared) for its instances.

0
source

This makes sense, although this is not an interesting practice. A static variable is usually intended to force the use of a single variable declaration during instance creation. An object is a new copy of a class with a different name. Despite the fact that the object is a new copy of the class, it still has the characteristics of a (unreasonable) class (first invisible instance). Therefore, the new object also has static members pointing to the original copy. Note the following: StackOverflow is also a new instance of StackOverflow.

0
source

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


All Articles