Static lifecycle, and scope of use

Source: Internet
Author: User

================================================================


First, the static usage is:
1.static the methods and variables that are decorated belong to the class itself, not to the object, regardless of how many object instances are created to share a method and variable.
2. Static methods can not call non-static methods, Non-static methods can invoke static methods.
Secondly
In your first program, main () is a static method, it calls the Yes_no () method, the Yes_no method must be static, so the first one to use static.
Again
The second method is to invoke Printlnfo () directly from the object by calling its internal method, not the main () method. So you don't need to use static modification.
I don't know if you understand that.


 There are two sections of programs: public class basic{public static void Main (string[] args) {yes_no (2008); ###### void Yes_no (int year) {if year%4==0&&year%100!=0| | year%400==0) System.out.print (year+) is a leap year.
  "); else System.out.print (year+) is not a leap year.
 ");
 -----------------------------------------------------------------public class basic{String name;
 int age;
  Public Basic (String Name,int age) {this.name=name;
 This.age=age;
 The public static void Printinfo () {System.out.println (name+) already has the "+age+" age. ");}
  Public ###### void Main (string[] args) {String name= "Jim";
  int age=19;
  Basic temp = new Basic (name,age);
 Temp.printinfo (); }
}

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.