I. Overview
The span style= "font-size:18px" > static keyword is used for static methods that declare static variables. Static means variables and functions that belong to the class and are not part of the class object.
When creating objects, unless you create objects of that class with new, Otherwise, you don't actually get any objects. The data storage space is allocated only when you execute new to create the object, and the method is called by the outside world. However, there are two cases where the above method cannot be resolved
The first one wants to allocate a single storage space for a particular domain without thinking about how many objects to create, or even creating any objects.
The second kind of person who wants a method (variable) that does not contain its class is associated with the object. That is, you can call this method without creating an object.
The static keyword was born to solve the two above problems.
When a thing is static it means that the field or method is not associated with any object instance of the class that contains it. Even if you have never created any objects of a class, you can call its static method or its static domain.
example.
Teststatic.java
public class Teststatic {public static int i=33;}
Staticmain.java
public class Staticmain {/** * @param args */public static void main (string[] args) {System.out.println ("teststatic.i=" +te STSTATIC.I); Teststatic s1=new teststatic (); Teststatic s2=new teststatic (); System.out.println ("s1.i=" +s1.i); System.out.println ("s2.i=" +s2.i); s2.i=133; System.out.println ("s1.i=" +s1.i); System.out.println ("s2.i=" +s2.i);}
:
Iv. Summary.
in the example we find that the first printout of the value of I is the realization of the person who wants a variable to not contain its class to be associated with the object. That is, you can call this method without creating an object. The following is an implementation that only wants to allocate a single storage space for a particular domain without thinking about how many objects to create, or even creating any objects. The static keyword solves both of these problems for us.
The static keyword still needs to be used sparingly to avoid unnecessary inconvenience to the program.
Getting Started with Java and mastering--The Static keyword of the basic article