Description of the static keyword in Java

Source: Internet
Author: User

The static keyword has two main functions:

First, a specific data type or object is assigned a single storage space, regardless of the number of objects created.

Second, implement a method or property associated with a class rather than an object

Specifically, in the Java language, static is primarily used in 4 cases: member variables, member methods, code blocks, and inner classes

(1) static member variable:

The Java class provides two types of variables: static variables decorated with the static keyword and instance variables that are not decorated with the static keyword. Static variables belong to the class, only one copy in memory, as long as the static variable is loaded, the static variable will be allocated space, so it can be used. There are two ways to reference a static variable, namely, "class. Static variable" and "object. Static variable"

An instance variable belongs to an object, and only when the object is created does the instance variable be allocated memory space to be used, it has multiple copies in memory, and is referenced only by the "object. Instance variable" method.

(2) static Member method:

Static methods and non-static methods are available in Java. The static method is a method of a class that can be called without creating an object, rather than as a method of an object, which can only be used if the object is created.

The This and Super keywords cannot be used in the static method, cannot call a non-static method, can only access static member variables and member methods of the owning class, because when the static method is called, the object of this class may not have been created, even if it has already been created. There is no way to determine which object to invoke. Similarly, a static method cannot access a variable of a non-static type.

Single Case design mode:

A very important use of static is to implement a singleton design pattern. The simple interest pattern is characterized by the fact that the class can only have one instance, in order to implement this function, the constructor of the class must be hidden, that is, to declare the constructor as private, and to provide a way to create the object, because the construction object is declared private, the outside world cannot directly create this type of object. Only through the methods provided by the class to get the object of the class, to achieve such a purpose can only declare the method of creating the object as static, the program instance is as follows:

Class Singleton{private static Singleton instance=null;private Singleton () {}public static Singleton getinstance () {if ( Instance==null) {instance=new Singleton ();} return instance;}}

  



(3) Static code block

Static code blocks are blocks of code that are independent of member variables and member functions in a class. Note: These static code blocks will only be executed once

(4) Static and final use the meaning of the expression:

For variables, using the static final decoration means that once the assignment cannot be modified and is accessible through the class name.

For methods, using the static final decoration means that the method is not overwritten and can be accessed directly through the class name.

  

The results of the above program operation:

A 0 B 1 C 2 D compile failed

Answer: D

In the Java language, you cannot define a static variable inside a member function

Description of the static keyword in Java

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.