[java] static keyword

1.static keyword

1.1 static variables

The characteristics of traditional attributes: they are stored in memory and each object has its own attributes.

When defining a class, it just describes the characteristics and behaviors of a certain kind of things, and does not produce specific data. Only after the instantiated object of this class is created through the new keyword, the system will allocate space for each object and store its own data. Sometimes, we hope that some specific data can only be shared in memory by all instance objects of a class. For example, if all students of a college contribute the same student name, security does not need to define a variable to represent the student name in the space occupied by each student object, but can define a variable to represent the school name in the space outside the object for all objects to share.

In a java class, you can use the static keyword to decorate a member variable, which is called a static variable. Static variables are shared by all instances and can be accessed in the form of "class name. Variable name". Static attribute, also known as class attribute, is stored in the memory of global data, so all objects can access the data area.

class Student{
    static String schoolName;
public class Test {
    public static void main(String[] args) {
        Student stu1=new Student();

Note: the static keyword can only be used to decorate member variables, not local variables. It is responsible for compiling errors.

Bad code instance:

class Student{
    public void study(){
        static int num=10;  //Illegal code, compilation error

Conclusion: class name and attribute name should be used to access static attribute (class attribute);

All non static properties (instance variables) must be used after the object is instantiated, while static properties (class properties) are not controlled by the object instantiation.

When defining a class, how to select instance variables and static variables?

When defining a class, 99% of the cases do not consider the static attribute, mainly the non static attribute (and instance object)

If you need to describe the concept of shared properties or are not controlled by object instantiation, use static

1.2 static keyword can be used to modify attributes, methods, code blocks and classes

When modifying methods, pay attention to:

Note 1. Static methods are not related to instances, but to classes, so this leads to two situations:

  • Static methods cannot directly use non static data members or calls
  • This and super cannot be used in static context (this is the reference of the current instance, super is the reference of the current instance's parent class, and is also related to the current instance)

Note 2:

The methods we used to write are simple and unified with static. But in fact, whether a method has static or not depends on the situation.

The main method is static.


class Person{
    //Instance variables, stored in memory
    public int age;
    //Instance variables
    public String name;
    //Instance variables
    public String sex;
    //Class variables are also called static variables. They have been generated at compile time. They belong to the class itself and only have one copy. They are stored in the method area
    public static int count;
    //Those decorated with final are called constants and also objects. They are decorated with final and cannot be changed later
    public final int SIZE=10;
    //Static constant, belonging to the class itself, has only one copy, which is modified by final and cannot be changed later
    public static final int COUNT=99;
    //Instance member method
    public void eat(){
        //local variable
        int a=10;

    //Instance member method
    public void sleep(){
        //local variable
        int a=10;
    //Static member method
    public static void staticTest(){
        //Cannot access non static member
        //set="men"; / / wrong

public class Practice {
    public static void main(String[] args) {
        //Generating objects, instancing objects
        Person1 p=new Person1();
        System.out.println(p.age);  //The default value is 0
        System.out.println(p.name);  //The default value is null
        //System.out.println(p.count); / / there will be a warning
        //The right way to access
        //Summary: all methods or properties decorated by static do not depend on objects

The result is:

Memory layout for data properties:

Published 53 original articles, won praise 8, visited 8864
Private letter follow

Tags: Attribute Java

Posted on Mon, 13 Jan 2020 03:12:22 -0800 by harlequeen