Introduction to the Java language Tutorial (vi): Static modifiers in the Java language

Source: Internet
Author: User
Tags modifier modifiers

In Java classes, we often see static keywords, which are often referred to as statics. The static modifier can be used to decorate a data member, a method member, but not a class (this refers to an external class) and cannot modify a construction method. For example,

package com.csst.vo;

public class User {

private String name;

private static int count;

Public String getName () {

return name;

}

public void SetName (String name) {

THIS.name = name;

}

public static int GetCount () {

return count;

}

public static void Main (string[] args) {

User user1=new User ();
User User2=new User ();

User1.setname ("Rose");

User2.setname ("Kate");

user1.count=10;

User2.count=20;

System.out.println ("User1.name:" +user1.getname () + "User2.name:" +user2.getname ());

System.out.println ("User1.count:" +user.getcount () + "User2.count:" +user2.count);

}
}

The results of this program are:

user1.name:Rose user2.name:Kate

user1.count:20 user2.count:20 

We can see that the User1 and User2 name properties are different, specified for the object invocation SetName, while User1 and User2 specify different values for the Count property, but the final output count is 20. The reason is that count is decorated with static, and name does not use count decoration.

A data member or method member decorated with a static modifier is called a static member, or a class member that is not modified by static, and is called an instance member. The static member is initialized only once, all objects are shared, so count will output two 20. Instance members are initialized only when the object is initialized, and each time an object is created, it is initialized once. Name was initialized 2 times when User1 and User2 were created, so the final output was Rose and Kate.

In addition, static members, because they are shared by the class, can be invoked not only with object invocations, but also with class names. In the static method, Non-static members cannot be used directly, and if used, the object must be initialized and invoked. If you have the following code in the User.java of the example above, a compilation error occurs.

public static void test(){

       setName("test");

}

Can be modified as follows:

public static void test(){

              User user=new User();

              user.setName("test");

}

Do not blindly declare a member as static, must understand the characteristics of static members, otherwise it will cause even though the syntax is correct, but the consequences of logic errors.

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.