Why is there a problem with a non-static variable being read from main?

前端 未结 3 1846
粉色の甜心
粉色の甜心 2020-12-07 01:28
String name = \"Marcus\";
static String s_name = \"Peter\";

public static void main(String[] args) {    
    System.out.println(name);//ERROR
    System.out.println         


        
3条回答
  •  再見小時候
    2020-12-07 02:10

    name is an instance variable in this case, and you are trying to access it without an object created, so technically name variable doesn't exist in memory, but for a static variable(s_name), its a class variable, it comes into existence once the class is created.

提交回复
热议问题