Statickeyword in Java is explained in detail

Source: Internet
Author: User

1.statickeyword 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 inner classes


There is no concept of global variables in 3.Java. However, 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, only the class where the static variable resides is loaded. will be allocated space, so it is used to pass classes. Static variables or objects. Static variables to reference

An instance variable belongs to an object, and it must be new out of the object and then referenced by an object. Instance variable, only the object will be created to assign space to him.

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


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

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

public class Singleton {   private static Singleton instance=null;//declares a static class variable   private Singleton () {};//Construction method Privatization, External cannot generate an object instance from new 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, if we do not need to statickeyword, then outside we need to call the getinstance () method to create the instance, then we need to first new object. obtained through object. Method name, but our goal is not to generate multiple objects through new, so we need to add Statickeyword, 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 member variables and methods, and he is not in any method body, the JVM runs a block of code when it loads the class. Suppose you have multiple blocks of code that run sequentially. Static code blocks are often used to initialize static variables. It is important to note that the static code block will only be run once!!!


6.static Inner class

The static inner class refers to an inner class that is decorated as static. He is not dependent on an external class instance object and is instantiated, and the usual inner class requires an external class instantiation of talent instantiation. The static inner class cannot have the same name as the outside, and cannot access the member variables of the external class, only the static member variable and the static method of the external class contain 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?










Statickeyword in Java is explained in detail

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.