I recently looked through a page on javarevisited and found a block of code that asked readers to determine what would be the result for it ...
Although I got the result, I am not happy with the result (WHICH CAME OUT TO BE "Hello"), since I do not know how to access the static member from a null reference. What happens in the background?
public class StaticDEMO { private static String GREET = "Hello"; public static void main(String[] args) { StaticDEMO demo = null; System.out.println(demo.GREET); // TODO code application logic here } }
, JVM , . demo ( a StaticDEMO), , GREET.
demo
StaticDEMO
GREET
, ( , , , - ). , , ( , !).
, :
System.out.println(StaticDEMO.GREET);
Java: 15, 11: .
15.11.1-2., (),[ ]
15.11.1-2.
, (),
[ ]
, Java, , memeber . , . / , - . , , , , - .
Static elements are stored with the class, and not with any particular instance. So it doesn't matter that the instance is null - the member from the class is still available.
The JVM simply ignores nullit because it GREETis a class field, but demoa non-reference field Class.
null
Class
StaticIt does not require an object reference to call it, so you can name it, even an object reference null.
Static
Source: https://habr.com/ru/post/1570254/More articles:Skomorphic keys for keyboard documentation - javascriptSimple gestures Kivy (swipes) - kivyWhy does String.intern () behave differently in Oracle JDK 1.7? - javaiOS8: how to download xib from Dynamic Framework - iosWebpage cleaning: automatic button click - javascriptЗапрос на отправку по многостраничной/форме данных Swift - phpParsing user agent OS version empty? - linuxThe default argument argument is replaced when re-declared - c ++Java Integer - javaПравильный разработчик. подход для переопределения шаблонов Twig - phpAll Articles