Accessing a static variable via an object reference in Java

前端 未结 6 1429
耶瑟儿~
耶瑟儿~ 2020-11-29 11:26

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

public class Static {
    private static String x = \"Static variab         


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 12:15

    The non-static member is instance member. The static member(class wide) could not access instance members because, there are no way to determine which instance owns any specific non-static members.

    The instance object could always refers to static members as it belongs to class which global(shared) to its instances.

提交回复
热议问题