This is the 12th day of my participation in the August Text Challenge.More challenges in August

The article directories

  • Object creation process
  • Java data transfer
  • Access control character

Object creation process

There are two classes A and B. B inherits from A

class A {
    int v1 = 1;
    static int v2 = 2;

    static{}public A(a) {}}Copy the code
class B extends A {
    int v3 = 3;
    static int v4 = 4;

    static{}public B(a) {}}Copy the code

Create instance B

B b = new B();
Copy the code

The object creation process is as follows: 1, load the parent class (A), static variables are allocated memory into the superclass (v2 allocated memory, but he didn’t run assignment algorithms, the default value is 0) 2, load subclasses (B), static variables are allocated memory for the subclass (v4 allocated memory, but he didn’t run assignment algorithms, The default value is 0) 2, perform parent static variable assignment and static initialization block (v2 assignment and initialization block, in code order) 4, perform subclass static variable assignment and static initialization block (V4 assignment and initialization block, in code order) 5, create parent object, Create a subclass object, and allocate memory for a subclass nonstatic variable (v3) 7. Perform the parent class nonstatic variable assignment operation 8

Program verification:

class A {
    int v1 = 1;
    static int v2 = 2;

    static {
        System.out.println("A static initialization block");
    }

    public A(a) {
        System.out.println("A construction method"); }}Copy the code
class B extends A {
    int v3 = 3;
    static int v4 = 4;

    static {
        System.out.println("B static initialization block");
    }

    public B(a) {
        System.out.println("B construction method"); }}Copy the code
public class Main {
    public static void main(String[] args) {
        newB(); }}Copy the code

Run the program:

Java data transfer

Java data types fall into two broad categories: Primitive types and Reference types.



Data passing of primitive data types is passing the value itself. The value of the reference type is passed, passing the memory address. Look at the following example:

The basic type variable a = 10, we pass the variable a into the f() method to reassign 100, and then print a, resulting in the previous 10

public class Main {
    public static void main(String[] args) {
        int a = 10;
        f(a);
        System.out.println(a);/ / output 10
    }

    private static void f(int a) {
        a = 100; }}Copy the code

Reference types we used the Point class before

public class Point {
    int x;
    int y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y; }... }Copy the code

Create a Point object a and assign a.x = 3, a.y = 4. Pass a into f() and re-assign a.x = 30, a.y = 40

public class Test {
	public static void main(String[] args){
		Point a = new Point(3.4);
		f(a);
		System.out.println(a.x);/ / 30
		System.out.println(a.y);/ / 40
	}

	private static void f(Point a) {
		a.x = 30;
		a.y = 40; }}Copy the code

Access control character

Java provides four types of access control characters (ACCs), which are used to control whether other classes can access properties or methods in a class, thus achieving data encapsulation. Divided into:

public > protected > friendly > private

Rule of selection: Try to narrow the scope of access

The modifier This class Classes in the same package A subclass Other classes
public You can visit You can visit You can visit You can visit
protected You can visit You can visit You can visit Can not access
The default You can visit You can visit Can not access Can not access
private You can visit Can not access Can not access Can not access

Private: If a member of a class (including member variables, methods, constructors, etc.) is decorated with private, that member can only be accessed inside the current class. Obviously, this access control is best used to modify member variables so that they can be hidden inside the class if the outside world accesses them. You can provide getter\ setter-like methods to access member variables that are private. Look at the following example

Student

public class Student {
    private int id;
    private String name;
    private String gender;
    private int age;

    public Student(a) {}public Student(int id, String name, String gender, int age) {
        this.id = id;
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId(a) {
        return id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName(a) {
        return name;
    }

    public String getGender(a) {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge(a) {
        return age;
    }

    public void setAge(int age) {
        this.age = age; }}Copy the code

Main.java

public class Main {
    public static void main(String[] args) {
        Student s = new Student();
        s.setId(9527);
        s.setName("A");
        s.setGender("Male");
        s.setAge(11); System.out.println(s.getId()); System.out.println(s.getName()); System.out.println(s.getGender()); System.out.println(s.getAge()); }}Copy the code

Run the program:



Default (Package access): If a member (including member variables, methods, constructors, etc.) or an external class does not apply any access-control modifier, it is called package access. The default access-control member or external class can be accessed by other classes in the same package.

Protected (subclass access) : If a member (including member variables, methods, constructors, etc.) is decorated with protected access control, that member can be accessed by other classes in the same package. It can also be accessed by subclasses in different packages. In general, when you use protected to decorate a method, you want subclasses to override the method.

Public (public access) : this is one of the most easy access level, if one member (including the member variables, methods, constructors, etc.) or an external class USES public access control to decorate, then the member or external classes can be all classes access, whether access and accessed in the same package. Whether there is a paternity relationship.