Accessing a static field with a NULL object in Java

前端 未结 3 587
独厮守ぢ
独厮守ぢ 2020-12-11 02:52

The following simple code snippet is working fine and is accessing a static field with a null object.

final class TestNull
{
    public static int field=100;         


        
3条回答
  •  误落风尘
    2020-12-11 03:24

    Static fields are associated with the class not an instance of that class. Therefore you don't need an instance of an object to access the static field.

    You could also access the field by calling TestNull.field

提交回复
热议问题