The static keyword in Java is detailed

Source: Internet
Author: User

The 1.static keyword has 2 main functions :

① allocates a single storage space for a particular data type or object, regardless of the number of objects created.

② can directly invoke a method or use the properties of a class directly from the class name without creating an object .

There are 4 main uses of 2.static: member variables (attributes), member methods, code blocks, and internal classes

There is no concept of global variables in 3.Java, but the effect of global variables can be achieved by static.

There are 2 types of variables available in Java: ① static variable with static modifier ② instance variable

Their difference is that the static variable belongs to the class, as long as the static variable where the class is loaded, it will be allocated space, so it can be used by the class. Static variables or objects. Static variables to reference

An instance variable belongs to an object, and you must first make a new object and then reference it through an object. Instance variable, and only the object will be created to allocate space.

★ It is particularly important that the static variable has only one, is owned by the class, all objects share this static variable and the static variable cannot be defined in the method!!!!!

4.static member methods, similar to variables, the static method belongs to the method of the class, and you can use the class name without creating an object. Static method invocation, cannot access non-static methods and variables in the static method, cannot appear this or Super keyword.

One of the most important applications of static is to implement a singleton pattern. The characteristic of a singleton pattern is that there can be only one instance.

public class Singleton {   private static Singleton instance=null;//declares a static class variable   private Singleton () {};//Construction method Privatization, External cannot generate object instance public   static Singleton getinstance () {//Global access point, provides a method for generating an instance of the class   if (instance==null) {   Instance=new Singleton ();   } return instance;}   }
Note that the method here is static, assuming that we do not use the static keyword, then outside we need to call the getinstance () method to create the instance, then the first new object, obtained through object. Method name, but our goal is not to generate multiple objects through new, so we need to add the static keyword to complete, directly through Singleton.getinstance () to create a unique instance of this class.

5.static Decorated code block

The static code block is independent of the member variables and methods, and he is not in any method body, the JVM executes the code block when the class is loaded, and if more than one block of code is executed sequentially, the static code block is usually used to initialize the static variable, and it is important to note that The static code block will only be executed once!!!

6.static Inner class

The static inner class refers to an inner class that is decorated as static and is instantiated without relying on an external class instance object, and the usual inner class requires an external class instantiation to instantiate. The static inner class cannot have the same name as the outside, and cannot access the member variables of the outer class, only the static member variables of the outer class and the static method include the private type.

7. Question?

1. What is an instance variable? What is local quantity? What is a class variable? What is a final variable?

What does 2.static final combination mean?

The static keyword in Java is detailed

Related Article

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: 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.