When using the method declared by the static type, it is important to note that if a static is declared in the class
Type, this property can be used in methods of non- static types, or in static
Used in methods of type. But when you invoke a property of a non-static type with a property of type static , you
An error occurred.
The code is as follows:
public class personstatic{string name = "Zhang San", static string city = "China"; int age;p ublic personstatic (String name,int age) { this.name = name; this.age = age;} public static void print () {System.out.println (name);} Public String Talk () {return "I am:" +this.name+ ", this year:" +this.age+ "years, from:" +city;}
Test.java:1: Error:class personstatic is public, should being declared in a file named Personstatic.javapublic class PersonS Tatic ^test.java:13:error:non-static variable name cannot be referenced from a static contextSystem.out.println (nam e); ^2 errors
Java--static declaration Method considerations