Home >Java >javaTutorial >Java basic introductory essay (8) JavaSE version - static static

Java basic introductory essay (8) JavaSE version - static static

黄舟
黄舟Original
2016-12-22 13:08:051378browse

Object-oriented (2)

this: represents the object. Which object does it represent? current object.

When member variables and local variables have the same name, you can use the keyword this to distinguish them.

This is the reference to the object to which the function belongs. (Simply put: which object calls the function where this is located, this represents that object.)

this can also be used to call other constructors in the constructor. Note: It can only be defined on the first line of the constructor. Because the initialization action must be performed first.

                                                                                                                                                                                                                                          person (String name) Objects of this class usually use this.

static:

Features: 1.static is a modifier used to modify members.

2. The members modified by static are shared by all objects.

3. Static exists prior to objects, because static members already exist as the class is loaded.修 4. Static modified members can be called directly by the class name, such as: class name. Member variables.

5. The data modified by static is shared data, and the time-specific data stored in the object is.

Note: Member variables are also called instance variables; variables modified by static are called static variables or class variables.

The difference between member variables and static variables:

1. The life cycles of the two variables are different.

Member variables exist with the creation of the object, and are recycled and released as the object is recycled;

Static variables exist with the loading of the class, and disappear with the disappearance of the class.

2. The calling methods are different.

Member variables can only be called by objects;

Static variables can be called by objects and class names (recommended).

3. The aliases are different.

Member variables are called instance variables;

Static variables are called class variables.

4. The data storage location is different.

Member variable data is stored in objects in heap memory, so it is also called object-specific data.

Static variable data is stored in the static area of ​​the method area (shared data area), so it is also called the shared data of the object.

Notes on static use:

1. Static methods can only access static members. (Non-static can access both static and non-static) (Note: Note that in the environment of the same class, static methods can only call static members of this class)

2. Cannot be used in static methods this or super keyword.

3. The main function is static.

When to use static?

1. Static variables.

When all the member variables in the analysis object are the same, then this member variable can be statically modified.

As long as the data is different in the object, it is the unique data of the object, which must be stored in the object and is non-static.

If it is the same data, the object does not need to be modified, it only needs to be used. It does not need to be stored in the object and is defined as static.

2. Static function.

Whether the function is statically modified, the only point of reference is whether the function has access to unique data in the object.

To put it simply, from the source code, whether the function needs to access non-static member variables, if so, the function is non-static. If not, the function can be defined as static.

Of course, this function can also be defined as non-static, but non-static needs to be called by an object, and only creating an object to call non-static methods without accessing unique data is meaningless when creating the object.

Special features of the main function:

1. The format is fixed; 2. It is recognized and called by jvm.

public: Because the permissions must be the greatest.

static: If you don’t need an object, just call it directly with the class name of the main function.

void: The main function has no specific return value.

main: function name, not a keyword, just a fixed name recognized by jvm.

String[] args: This is the parameter list of the main function. It is an array type parameter, and the elements are all string types. (It can be passed in when calling the main function, for example: java mainDemo xx yy zz is a string array with 3 elements passed in)

Static code block construction code block local code block:

Static code block: With Executed when the class is loaded. And only executed once. Function: Used to initialize the class.

Construction code block: executed as the object is created, and called several times when the object is created. Function: Can be initialized for all objects. (The difference from the constructor: The constructor performs targeted initialization of the corresponding object. The construction code block object initialization is universal.)

The role of local code blocks: limit the life cycle of local variables.

The calling sequence of the three is as follows:

class StaticCode
{
    static int num ;
    //静态代码块 (首先执行)
    static
    {
        num = 10;
//      num *=3;
        System.out.println("hahahah");
    }
    StaticCode(){}
 
    static void show()
    {
        System.out.println(num);
    }
}
 
class Person
{
    private String name;
     
     
 
    {//构造代码块。可以给所有对象进行初始化的。(如有调用对象,即其次调用,与局部代码块视情况而定)
 
        System.out.println("constructor code ");
//      cry();
    }
     
    static
    {
        System.out.println("static code");
    }
     
    Person()//是给对应的对象进行针对性的初始化。 
    {
        name = "baby";
//      cry();
    }
    Person(String name)
    {
        this.name  = name;
//      cry();
    }
    public void cry()
    {
        System.out.println("哇哇");
         
    }
 
    public void speak()
    {
        System.out.println("name:"+name);
    }
 
    static void show()
    {
        System.out.println("show run");
    }
}
 
 
class StaticCodeDemo 
{
    static
    {
//      System.out.println("a"); //先执行
    }
    public static void main(String[] args) 
    {
 
//      Person p = null;
//      p.speak();
 
//      Person.show();
//      Person p1 = new Person();
//      p1.speak();
//      Person p2 = new Person("旺财");
//      p2.speak();
//      new Person();
         
 
//      new StaticCode().show();
//      new StaticCode().show();
//      StaticCode.show();
//      System.out.println("b");    //后执行
    }
}

The above is the basic introduction to Java essay (8) JavaSE version - static content. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn