Antecedents feed

Only static members can be in initalizers

What?? 😮 double number = generateData (); Can you write ☕ for the dart class? What 🤔 for Java?

Positive 🎉

The Java class declaration

public class Test {
    // Call the member method and assign the return value to the member variable num
    public double number = this.generateData();
    private String name;

    // Initialize the code block
    {
        System.out.println("---- initializes block ----" + this);
    }

    // The constructor
    public Test(String name) {
        this.name = name;
        System.out.println("Constructor ----" + this);
    }

    private double generateData(a) {
        System.out.println("---generateData---");
        return Math.random() * 100;
    }

    @Override
    public String toString(a) {
        return "Test{" +
                "number=" + number +
                ", name='" + name + '\' ' +
                '} '; }}Copy the code

In a class this is a reference to an instance object of the current class. In Java classes this can appear in member variable initializers, initialization blocks, constructors, and member methods.

The Dart class declaration

import 'dart:math';
class Test {
  / / 0 ️ ⃣
  double number=this.generateData();
  String name;

  Test(String name)
      : number = 10.0,
        name = "jack" {
    print('number=$number');
  }

  double generateData()=>Random(10).nextDouble();
}

Copy the code

It looks similar to Java, but the code is already reporting an error at 0️


This reference to this is invalid



Initialization can only access static members
Use the members of the column


To reassure 😎

To explain this, let’s first talk about what the Dart class consists of










💡 If you initialize an instance variable when you declare it (rather than in a constructor or other method), then the value of the instance variable will be set when the object instance is created, before the constructor and its initializer list are executed. The dart class instance variable ⚠ constructor has not yet been executed and the instance object has not yet been initialized. Therefore, none of the instance members can be accessed, but the class members can. Dart class constructor

From 💡 and ⚠ above, we can see that when an instance variable is assigned before the constructor, the instance object is not initialized before the constructor, so the non-assignment phase cannot call the member method. ** Summary: ** The places in the Dart class where this can be accessed are to the left of the equals sign of the component, member method, and initialization list.

Confused and 😣

Dart doesn’t, so why does Java?

public static void main(String[] args) {
        Test test = new Test("kk");
    }
Copy the code

This is one way to create objects in Java. The process of creating an object is not as simple as executing a constructor. 1. The home page should determine whether the class has been loaded, if not, start loading operation 2. 3. The Java virtual machine invokes a method to initialize the object, which corresponds to the constructor. This method is automatically created, as can be seen by looking at the bytecode of the Javap -v class name.










Refer to the link

Dart Development Language Overview [Java Interview Questions] Java object initialization details with class initialization