Package + static member + code block + internal class

Posted by ayfine on Sun, 19 Sep 2021 18:08:53 +0200

1, Package

1. In order to better manage classes, collect multiple classes into a group and become a software package

  • Package is the embodiment of the encapsulation mechanism of classes and interfaces. It is a better management of classes or interfaces
  • Classes with the same name are not allowed in the same package, but they can exist in different packages

2. Import the classes in the package

import java.util.Date;
public class TestDate1 {
    public static void main(String[] args) {
        //Import classes in package
        //Method 1 (take importing a timestamp as an example)

        java.util.Date date = new java.util.Date();
        System.out.println(date.getTime());

        //Method 2 (import package with import statement)
        Date date1 = new Date();
        System.out.println(date1.getTime());

        Date date2 = new Date();
        System.out.println(date2.getTime());
    }
}

3. Custom package

  • Add a package statement at the top of the file to specify which package the code is in
  • The package name should be specified as a unique name as far as possible
  • The package name should match the code path. For example, if you create a package of com.bit.demo1, there will be a corresponding path com/bit/demo1 to store the code
  • If a class does not have a package statement, the class is placed in a default package
  • Importing a custom package can be done with the import statement, just like importing a package

4. Access control of package

2, static member

Take the student class as an example

public class student {
    public String name;
    public String gender;
    public int age;

    public student(String name, String gender, int age) {
        this.name = name;
        this.gender = gender;
        this.age = age;
    }
    public void print(){
        System.out.println(name + "-" + gender +"-" + age);
    }

    public static void main(String[] args) {
        student a = new student("Zhang San","male",20);
        student b = new student("Li Si","female",21);
        student c = new student("Wang Wu","male",22);
        a.print();
        b.print();
        c.print();
    }
}

For the member variables defined in the student class, each object will contain one (instance variable)

1. In Java, the member modified by static is called static member variable, also known as class member variable. It does not belong to any object. It is private and shared by all objects

Properties of static member variables:

  • It does not belong to a specific object, but is a class attribute. All objects share it and are not stored in the space of an object
  • It can be accessed by object or class name, but it is generally recommended to use class name
  •   JDK7 and before, HotSpot(Java virtual machine) is stored in the method area, JDK8 and after, and class variables are stored in the Java heap
  • The life cycle follows the life of the class (that is, it is created with the loading of the class and destroyed with the unloading of the class)
  • It can be accessed directly by class name or by object  

2.static modifier member method

  • It does not belong to a specific object, but is a class method
  •   It can be called by object or by class name. Static method name (...)
  • Static methods do not have hidden this reference parameters, so you cannot access any non static member variables in static methods
  • No static method can be invoked in static methods, because non static methods have this parameters, and this references can not be passed when calling in static methods.

3.static member variable initialization

  • Local initialization

    Is to give the initial value directly at the time of definition

  • Static code block initialization

  3, Code block

A piece of code defined with {} is called a code block

  • Common code block

    Code blocks defined in methods

    public static void main(String[] args) {
            student s1 = new student("Zhang San","male",20);
            student s2 = new student("Li Si","female",21);
            student s3 = new student("Wang Wu","male",22);
            System.out.println(student.classroom);
            System.out.println(s1.classroom);
            System.out.println(s2.classroom);
            System.out.println(s3.classroom);
        }

    Defined in {}

  • Construction code block (instance code block, which is generally used to initialize example member variables)
    public class student1 {
            public String name;
            public String gender;
            public int age;
            public static String classroom = "101";
            public student1() {
                System.out.println("I am a parameterless construction method");
            }
       //Construction code block (instance code block)
        {
            this.name = "Zhang San";
            this.gender = "male";
            this.age = 20;
        }
            public void print(){
                System.out.println(name + "-" + gender +"-" + age);
            }
    
            public static void main(String[] args) {
                student1 s1 = new student1();
                s1.print();
            }
    }

    Note: instance code blocks are better than constructor execution  

  • Static code block

    Code blocks defined using static are called static code blocks, which are generally used to initialize static member variables

    Static code blocks are generated only once, no matter how many objects are generated  

    1. Static member variables are class attributes, so they are opened up and initialized when the JVM loads the class

    2. If a class contains multiple static code blocks, the compiler will compile the code according to the defined order   This method is finally placed in the generated < > method, which is called only once when the class is loaded

    3. The instance code block is executed only when the object is created

  • Synchronous code block

4, Inner class

An outclass is called an external class and an Innerclass is called an internal class  

Those modified by static are called static inner classes, and those not modified are called ordinary inner classes  

Topics: Java JavaSE