Definition and use of the static keyword

Overview of the static keyword

 

About static keyword, which can be used to modify the member variables and member methods , modified members belong to the class, rather than just part of an object. In other words, since belong to the class, you can not rely create objects called.

 

1.static static keyword

 

2.static can be used to modify the member variables and member methods

 

Definition and use format

Class variables

 

static modification member variables

When the static modification member variable when the variable is called a class variable . Each object class share the same class variable values. Operate on class variables without any object can change the value of such variables, but you can not create objects of that class in the.

Class variables : the static keyword modified member variable.

1. Definition Format:

static data type variable name; 
e.g. 
static String name;

2, features:

Modified static member variables will become static variables , static variables not only belong to just one object, but belong to the class, so static variables are shared by all objects of that class, which means that all objects of that class using the same data

 

3, static member variables of access:

Object name. Static member variable name

The class name. Static member variable names recommended

 

Static method

static modification member methods

1. Format:

In fact, the definition of the method when, in front of the return type of the static keyword to add value

Static modifier return type method name (parameter list) {

                 The method body

}

2. Static member method access:

     The object name static method name (arguments);

     Static class name method name (arguments); Recommended

3. Note:

     Non-static method can directly access both static and non-static member

     Static method can directly access only static members, can not directly access non-static member

     Static methods can not appear in this

     Static members: contains static member variables and static member methods

     Non-static member: non-static member variables and non-static member methods

 

static (Static) block

static can be used to modify the code block -> static code block

1. Format:

static { code block}

2 positions: an outer class methods

3. Execution:

With the loaded class is executed and performs a priority for performing the method of the main methods and constructors.

For the first time using the class will load the class, and only loaded once

 

Guess you like

Origin www.cnblogs.com/libinhong/p/10990107.html