Java object-oriented keyword static Getting Started instance

Source: Internet
Author: User

First, the basic concept

The static keyword static is a member modifier that is used directly to decorate a member.

(a) Characteristics:

1. The result of being statically modified can be called directly by the class name.

2. A static member takes precedence over the existence of an object.

3. Static members are loaded as the class loads. Disappears as the class disappears. A static member has a long life cycle.

(ii) Precautions:

1. Static methods can only access static members and cannot access non-static members.

2. The this or Super keyword cannot appear in a static method.

3. The main function is static.

(c) When to use:

1. Member variable: If the data is the same in all objects. Directly with static modification.

2. member function: If the function does not have access to the property data in the object, then the function is static.

(iv) The difference between a member variable and a static variable:

1. Name: A member variable is also called an instance variable. A static variable is also called a class variable.

2. Memory storage: Member variables are stored in the object of the heap memory. Static variables are stored in the static area of the method area.

3. Life cycle: Member variables appear as the object appears, disappearing as the object disappears. Static variables are loaded as the class loads. Disappears as the class disappears.

(v) Static code block:

1. Executes as the class loads, and executes only once.

2. Initialize the class.

3. The class does not need to create an object, but it needs to be initialized. This is to store part of the code in a static code block.

Second, the example code

classperson{PrivateString name; Private intAge ; Person (String name,intAge ) {         This. Name =name;  This. Age =Age ; }     Public voidShow () {System.out.println ("Name=" +name+ ", age" +Age ); }    //Since the sleep method does not have access to the properties in the object, you can modify the method with static. (Memory-saving use)     Public Static voidsleep () {System.out.println ("Sleep ..."); }    //Static code block    Static{System.out.println ("Static ..."); }}classstaticdemo{ Public Static voidMain (string[] args) {person P=NewPerson ("xiangming", 20); Person.sleep ();//call the Sleep method, which can be accessed by means of the class name. Person.sleep ();//a method called with the class name must be decorated with the specified modifier static    }}

Third, the code to run

  

Java object-oriented keyword static Getting Started instance

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.