This is the 14th day of my participation in the August More Text Challenge. For details, see:August is more challenging

Java Learning Notes series — Sun Bujian 1208

【 Detail + super basic 】Java- Learning Notes 01

【 Detail + super basic 】Java- Study Notes 02

【 Detail + super basic 】Java- Study Notes 03

【 Detail + super basic 】Java- Study Notes 04

【 detail + super basic 】Java- Study Notes 05

【 Detail + super basic 】Java- Study Notes 06

【 Detail + super basic 】Java- Study Notes 07

【 Detail + super basic 】Java- Study Notes 08

【 Detail + super basic 】Java- Study Notes 09

Ongoing updates….

The modifier

1.1 package and import

  • Packages are folders that manage class files

  • Package name; For example: package com.jumper. Demo;

  • With compiling & bags with compiling and running, javac, d. the name of the class. Java, for example: javac – d. com. Jumper. Demo. The HelloWorld. Java bag run: Java package name + the name of the class Such as: java com.jumper.demo.HelloWorld

  • When you use classes from different packages, you have to write the full path of the class, which is too cumbersome to write. In order to simplify the operation of the package, Java provides the functionality of the package

  • Format: import package name; Example: import java.util.scanner;

  • Sample code (Scanner object created without a tutorial)

packageCom. Heima;public class Demo {
	public static void main(String[] args) {
	// 1. Create a Scnaner object without a tutorial
java.util.Scanner sc = newJava. Util. Scanner System. (in);}}Copy the code
  • Sample code (Scanner object created after using the tutorial)
packageCom. Heima;importJava. Util. Scanner;public class Demo {
	public static void main(String[] args) {
	// 1. Create a Scnaner object without a tutorial
		Scanner sc = new Scanner(System.in);
	}
}
Copy the code

1.2 Access modifiers

We used public when we defined classes, private when we defined properties, and public when we defined methods. They are both Java access modifiers. Java defines four types of access control characters: private, default access, protected, and public. Access control can be controlled through the permission control compliance package.

  • private

    Private can be used to modify member variables, member methods, constructors, and inner classes (inner classes are classes defined in another class that are members of the same class as member variables). A privately-decorated member can only be accessed by other methods in the class to which it belongs, and not by other classes outside that class.

  • Default access Permission The default access permission is also called the package access permission. The default access permission is the one that does not write the access control character. Default access can be used to decorate member variables, member methods, constructors, classes, and interfaces. Default access defines content that can be accessed by classes in the same package, including itself, but not by classes in other packages.

  • Protected Protected can be used to modify member variables, member methods, constructors, and inner classes. Protected members can be accessed by other classes in the same package, including classes that are subclasses of different packages, but not classes in other packages that are not subclasses of the same package.

  • Public Public can be used to decorate member variables, member methods, constructors, and classes and interfaces. Pulie decorated members can be accessed by any class, whether or not they are in the same package.

Access table

The modifier The same class Subclass-independent classes in the same package Subclasses of different packages Extraneous classes for different packages
private Square root
The default Square root Square root
protected Square root Square root Square root
public Square root Square root Square root Square root

Class variables, instance variables and local variables

Variables defined inside a class and outside a method are called member variables, and variables defined inside a method are called local variables.

Member variables:

There are two types of member variables: class variables and instance variables. When a member variable is defined, it is called a class variable if it is preceded by the keyword static. If it is not preceded by the keyword static, it is called an instance variable. Class variables are stored in the common area of the class. They belong to the class and are shared by all objects of the class. Instance variables belong to objects, and each object has its own independent instance variables, which are stored in the object’s memory.

Local variables:

Variables defined in a method are called local variables. There are also two kinds of local variables: one is the parameter of a method, and the other is the variable defined in the body of the method. The life cycle of a local variable exists only within the method; when the method call ends, the local weight is gone.

A local variable in a method body must be assigned an initial value before it can be used. Otherwise, the compiler considers the variable uninitialized and cannot use it.

Instance methods and class methods

When a method is declared, an instance method is not static before its name, and a class method is static. When multiple objects are created using a class as a template, each object has its own independent instance method, but all objects share the class method of the class. (1) Instance method invocation.

Object name. Method ();Copy the code

(2) Class method calls. There are two formats for calling a class method: “object name. Method 0 or class name method 0. Class name method () is recommended.

** Example: ** write an employee class, assuming that all employees have the same base salary, but different allowances. The employee class has methods to return total salary and common salary.

public class Employee {
    static double wage;  // Base salary, static variable, all objects are the same
    double allowance;    // Allowance, instance variable, allowance may be different for each object
    public double getSalary(a) { // Instance method. You can use instance variables and class variables
        return wage+allowance;
    }
    public static void addWage(int a) {
        wage=wage+a;
        // allowance=allowance+a; Error: Because class methods can only use class variables
    }
    public static void main(String[] args) {
        Employee e1=new Employee();
        Employee.wage=1000;
        e1.allowance=200;
        Employee.addWage(500); System.out.println(e1.wage); System.out.println(e1.getSalary()); }}Copy the code

** Summary: ** Class methods belong to the class. They exist in the memory of the class when the class is first loaded and can be used without creating an object. Therefore, class methods can only use class variables because they exist when the class is loaded. Instance variables, however, do not exist in the object’s memory until after the object is created, so class methods cannot use instance variables. An instance method exists after the object is created. With an instance method, both class variables and instance variables must exist, so an instance method can use both class variables and instance variables.

1.1 Final keyword

  • Fianl keyword function

We can modify member methods, member variables, and classes

  • Final modifies the effect of classes, methods, and variables

Fianl modified class: this class cannot be inherited (there can be no subclasses, but there can be superclasses)

Final modifier: This method cannot be overridden

Final: indicates that the variable is a constant and cannot be assigned again

  • Fianl modifies variables of basic data types

The final modifier means that the data value of a primitive type cannot be changed

  • Final modifies reference data type variables

The final modifier means that the address value of a reference type cannot be changed, but the contents of the address can be changed

For example:

public static void main(String[] args){
	final Student s = new Student(23);
	s = new Student(24);/ / error
	s.setAge(24);/ / right
}
Copy the code

1.2 Static keyword

  • The concept of the static

The static keyword is used to describe member methods and variables.

  • Features of static modification

Is shared by all objects of the class, which is how we determine whether to use a static keyword

You can call it by the name of the class or you can call it by the name of the object.

Example code:

class Student {
	publicString name;/ / name
	public intage;/ / age
	public staticString university;// Schools share data! So the design is static!
	public void show(a) {
		System.out.println(name + "," + age + ","+ university);}}public class StaticDemo {
    public static void main(String[] args) {
        // Assign a value to the object's shared data
        Student.university = Shandong Jianzhu UniversityStudent s1 =newStudent (); s1. Name ="Sun Bujian"; s1. Age =18; s1.show(); Student s2 =newStudent (); s2. Name ="Sun Ming"; s2. Age =17; s2. The show ();}}Copy the code

Static access characteristics

  • Non-static member methods

Can access static member variables

Can access non-static member variables

Can access static member methods

Access to non-static member methods

  • Static member methods

Can access static member variables

Can access static member methods

Static member methods can only access static members

The initialization order of the object

  • Superclass static variable, superclass static code block, subclass static variable, subclass static code block, superclass non-static variable (instance variable), Superclass non-static code block, Superclass constructor (constructor), Subclass non-static variable, Subclass non-static code block, Subclass constructor (constructor)
  • Father after son, first static after non-static, first measure and then block after method.
The initial order of code blocks
class person{
    private String name;
    static {
        System.out.println("This is static block 1.");
    }
    {
        System.out.println("Here's the instance block.")}static {
        System.out.println("This is static block 2.");
    }
    public person (String name) {
        this.name=name;
         System.out.println("Here's the constructor."); }} This is a static block of code1This is a static block of code2This is the instance block and this is the constructorCopy the code

After the static block executes, the instance block (the constructor) executes, followed by the constructor. Static code blocks are executed according to the order in which we wrote the code.

Welcome to subscribe column to invite you to drink a cup of Java, hope to bring convenience to friends in need, but also hope to get everyone’s attention and support.