Java--static declaration Method considerations

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.