Static Continuation--

Source: Internet
Author: User

1, static means "global" or "static" meaning, used to modify the member variables and member methods, you can also form static code block, the static modified member variables and member methods independent of any object of the class. That is, it does not depend on class-specific instances, is shared by all instances of the class (so it can be used to count how many instanced objects a class has), so some properties want to be shared by all objects, and must be declared as a static property, and the property declared by static is a global property.

2. As long as this class is loaded, the Java virtual machine can find them based on the class name in the method area of the runtime data area. Therefore, a static object can be accessed before any of its objects are created, without referencing any objects.

3. Static code blocks defined in a class take precedence over the construction block, and no matter how many objects, static code blocks execute only once

Example 1:

Class persone{
private String name;
private int age;
private static String country = "a city";
Public Persone (String Name,int age) {
THIS.name = name;
This.age = age;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
public static String Getcountry () {
return country;
}
public static void Setcountry (String country) {
Persone.country =country;
}
public void info () {
System.out.println ("Name:" +this.getname () + "," + "Age:" +this.getage () + "," + "City:" +country ");
}
}
public class Staticuse {
public static void Main (string[] args) {
Persone per1 = Newpersone ("Zhang San", 20);
Persone per2 = Newpersone ("John Doe", 22);
System.out.println ("--------before---------modification");
Per1.info ();
Per2.info ();
System.out.println ("---------after---------modification");
Persone.setcountry ("b City");
Per1.info ();
Per2.info ();
}
}

Static Continuation--

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.