1. Class myclass {2. static string myname = "scjp"; 3. 4. Myclass getMyClass () {5. System.out.println (MyName); 6. Return NULL; 7.} 8. Public static void Main (String [] args) {10. System.out.println (new myclass (). getMyclass (). MyName); 11.} 12.
What do you think about this program?
After reading the process, give me the first impression that the sixth lines and 10th lines have some embarrassment. Some friends may say that members who use null objects to call the members of the class. This is definitely to throw an exception!!! But in fact, not this, this program does not have any errors, and it will output two strings "scjp" correctly. First, the variable myName is a Static member variable, that is, this variable is a class variable, it is The object does not matter. Use an object to call the Static member to call, and the object itself does not matter, so a reference is NULL, but as long as it belongs to that type, then it You can properly access Static members in the class.