Last time I wrote for you to “constructor and its overload _ method call”, the last article code may be more, if not thoroughly understood, please open the following article to watch oh!!

Object oriented 3 constructor and its overload _ method call

This time we’ll look at the use of the static keyword in object orientation

Learning tutorials recommended:

  • 1. Beijing Gaoqi Java300 Set (Java strongly recommended)

    Java300 sets zero basic tutorial for beginners – Java300 sets zero basic tutorial for beginners – Java300 sets zero basic tutorial for beginners – Java300 sets zero basic tutorial for beginners – Java300 sets zero basic tutorial for beginners – Java300 sets zero basic tutorial for beginners
  • 2.JavaSE Basics – Starting from scratch (recommended)

    Object Oriented Java Basics /JavaSE/ Object-oriented programming /OOP programming _bilibili
  • 3.Java Full Course – basic prerequisite

    JAVA Full course _Java introduction _Java Zero Foundation prerequisite _Java Programming course _Java Core Foundation _EasyUI_SSM Integration framework _redis_High Concurrency — Full course
  • 4.Java common class basic practice

    Java common class actual combat basic tutorial _Java eight common class core foundation _Java common class basic /Java wrapper class /String class
  • 5. Basic Mathematics knowledge for Java [Data Structure and Algorithm] (recommended)

    Data structure and algorithm _Java data structure and algorithm foundation to advanced /Java basic introduction to advanced /Java data structure analysis /Java data structure FAQ _ bilibili_bilibili
  • 6.Java object-oriented programming _OOP basic in-depth explanation

    OOP OOP OOP OOP OOP OOP OOP OOP OOP OOP OOP
  • 7.GOF23 kinds of design patterns – detailed explanation of design patterns in 23 courses

    Java GOF23 kinds of design patterns from singleton to memo mode 23 kinds of patterns in detail

.

【static keyword 】

Static is a keyword in Java. The word itself means static. Members of a class include variables, methods, constructors, code blocks, and inner classes. Static modifies all members except constructors.

Members decorated static become static members that belong to a class. Members that do not use the static modifier become instance members that belong to each object of the class.

.

Static variable:

In a class, member variables declared static are static member variables, also known as class variables. Class variables have the same life cycle as classes and remain in effect throughout application execution. It has the following characteristics:

  • Is a common variable of the class, belongs to the class, is shared by all instances of the class, and is explicitly initialized when the class is loaded.
  • There is only one static member variable for all objects of this class. Shared by all objects of this class!!
  • Class name. Class attribute/method. (Static members can also be accessed by object reference or class name (no instantiation required).)
  • Non-static members cannot be accessed directly in static methods.

Example code:

Public class Student {private int sno; // Private String name; // Name private String sex; // private double score; // private String cup; //private static String waterDispenser; // Private static String classRoom; //private static String teachName; Public Student(){} public Student(int sno, String name, String sex){this.sno = sno; this.name = name; this.sex = sex; } public void show(){ System.out.println(sno +" "+this.name+" "+sex+" " +this.score+" "+classRoom); } public static void main(String[] args) { System.out.println(Student.classRoom); Student.classRoom = "503"; System.out.println(Student.classRoom); Student stu = new Student(7," 三 "," 三 ",77); stu.sno = 5; //Student.sno = 6; stu.classRoom = "507"; stu.show(); Student stu2 = new Student(); stu2.show(); }}Copy the code

Static variable allocation in memory is shown as follows:

The difference between static and non-static variables:

First, the number of copies is different: static variable: only 1 copies; Non-static variables: 1 object per copy

Second: different storage locations: static variables: method area; Non-static variables: in the heap

Third, different times of memory allocation: static variables: when the class is first loaded; Non-static variable: When an object is created

Fourth: different life cycles. Static variables and classes have the same life cycle; Non-static variables have the same life cycle as the object they belong to

Fifth: different ways to call

  • Static variables: Call student.classroom from the class name or stu1 from the object name. ClassRoom =”301″ is not recommended
  • Non-static variable: call stu1.name =” zhang “by object name;

.

Static method

1. Static methods

Access static variables and static methods

2. Static method calls

Call student.showClassroom () with the class name; Recommended method

Access stu1.showClassroom () by object name;

The static method does not work

  • Non-static variables cannot be accessed in static methods
  • Non-static methods cannot be accessed in static methods
  • This is not accessible from static methods

Static variables and methods are loaded when a class is loaded. The object may not have been created yet, so non-static variables and methods have not been allocated space and cannot be accessed

4. Static methods can be used

  • Static variables can be accessed in non-static methods
  • Static methods can be accessed in non-static methods

When a class is loaded, static variables and methods are already loaded. After an object is created, non-static variables and methods are allocated space. Static variables and methods already exist and can be accessed

Let’s take a look at some sample code to use static methods:

Public class Student {// Member variable String name; int age; String sex; double score; static String classRoom; / / 103!!!!!! public static void showClassRoom(){ System.out.println(classRoom); //System.out.println(name); // non-static variable //shout(); // system.out.println (this); } public static void setClassRoom(String classRoom){ Student.classRoom = classRoom; Public void rule (){system.out.println (this.name+" "+this.age+" "+ sex+" "+score+" "+classRoom);} public void rule (){system.out.println (this.name+" "+ sex+" "+score+" "+classRoom); showClassRoom(); } public static void main(String[] args) { Student.showClassRoom(); Student.setClassRoom("r301"); Student.showClassRoom(); //Student stu1 = new Student("小王",20,"男",98); Student stu1 = new Student("小王",20,"男"); Stu1.name =" stu1.name "; stu1.classRoom ="r301"; stu1.showClassRoom(); }}Copy the code

.

Static code block

Local code blocks: features

  1. Location: in the method

  2. Quantity: multiple

  3. Execution sequence: Execute in sequence

  4. Variables defined in a local code block are limited in scope to the current code block

(Member) code block: Feature

  1. Location: in the class

  2. Quantity: multiple

  3. Execution sequence: Execute in sequence

  4. Execution time: executes each time an object is created; The code block is executed before the constructor is executed

Function: rarely used in actual development; You can extract code common to each constructor into a code block;

Note: Anonymous inner classes cannot provide constructors, so initialization is placed in the code block

Static code block: Features

  1. Location: in the class

  2. Quantity: multiple

  3. Execution sequence: Execute in sequence

  4. Execution time: the first time the class is loaded, only once

Assigns an initial value to a static variable. In actual development, it is used to perform some global initialization operations, such as creating factories and loading initial database information

Static code block Example code:

public class Student { static { System.out.println("static code block 1"); } String name; static String classRoom; / / 103!!!!!! static { //name = "sdf"; classRoom = "r301"; System.out.println("static code block 2"); } public Student(){ System.out.println("-----Student()---------"); } public static void main(String[] args) { int n = 4; // int m = 5; // int m = 5; // System.out.println(m); // } System.out.println(classRoom); new Student(); new Student(); } { //classRoom = "r301"; System.out.println(" code block 3"); }}Copy the code

The above is all the content of this chapter, I will update the follow-up oh, like the partner support oh ~

Thanks for watching ~