Ii. Java Object-oriented (5) _static modifier

Source: Internet
Author: User

2018-04-29

Shuyujingerfengbuzhi

static modifier

The static modifier represents the rest, which can be decorated fields, methods, inner classes . with this keyword-decorated content, static-decorated content in object-oriented objects is subordinate to the class, not directly to the object, so static-decorated member variables are generally referred to as class-member variables, whereas static-decorated methods are generally referred to as class methods.

The static modifier features:

1) TheMember (Field/method) of the static modifier that is loaded as the class is loaded.

    When the JVM loads the bytecode into the JVM, the static decorated members already exist in memory.

2) precedence over the existence of an object.

    The object is created by us manually through the new keyword.

3) satic decorated members are shared by all objects of that type.

Static members can be accessed based on any object created by the class.

Analysis: On the surface, the static members are accessed through objects, and their nature is still accessed using the class name, with no relation to the object (as can be seen by anti-compilation).

4) use the class name directly to access the static member.

    Because static decorated members directly belong to the class and do not belong to the object, you can access the static member directly using the class name.

Access to class members and instance members

1) Class members: Members that use the static modifier, directly belong to the class, and can access the static field and the static method through the class.

2) Instance members: Members that do not use static adornments, are objects only, access non-static fields through objects, and non-static methods (objects can actually access class members, but the underlying classes are still accessed using the class name).

3) static method: Only static members can be called.

4) Non-static method: You can access static members, or you can access instance members.

The difference between static and non-static:

(1)

A non-static member variable is also known as an instance variable.

Static member variables are also known as class variables.

(2)

A non-static member variable that exists as the object is created and disappears as the object disappears.

A static member variable that exists as the class is loaded and disappears as the class disappears.

(3)

Non-static variables exist in the object heap memory.

Static variables exist in the method area.

(4)

Non-static variables can only be called by the object.

A static variable can be called by a class name, or it can be called by an object.

When to define static fields and methods:

If this state or behavior belongs to the whole thing (class), it is directly used by the static modifier and is shared by all objects. In development, tool methods are often used for static retouching. If you do not use the static decoration, then these methods belong to the object of the class, we have to create the object and then call the method, in the development of the tool object only need a copy, may create n objects, at this time we tend to design the class as a singleton, but still a bit troublesome. Therefore, in the general design of tool methods in development, in order to invoke the simple, we use static decoration.

Storage of static members in the JVM:

code example:

1 class Person2     {3String name;//name4         intAge//Age5 6         Static intTotalnum = 5;//Total human population 57 8         //constructor Function9Person (String N,inta) {Tenname = N;//assign N to name Oneage = A;//assign a to age Atotalnum++; -         } -  the         voidDie () { -totalnum--; -System.out.println ("Death"); -         } +  -         //only humans can destroy . +         Static voiddestory () { ATotalnum = 0; atSystem.out.println ("Human Destruction"); -         } -     } -  -  - //demonstrating the static modifier in classPersonDemo2 - { to      +          Public Static voidMain (string[] args) -         { the System.out.println (person.totalnum); *person P1 =NewPerson ("Zhang San", 10); $person P2 =NewPerson ("John Doe", 5);Panax NotoginsengSystem.out.println (Person.totalnum);//invoking an object using a class -  theP2.die ();//P2 death + System.out.println (person.totalnum); A  the person.destory (); + System.out.println (person.totalnum); -     } $}

Output Result:

Ii. Java Object-oriented (5) _static modifier

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.