Java class definition and execution sequence tutorial

  • 2020-04-01 04:10:19
  • OfStack

Classes must be defined before they can be used. A class is a template for creating an object, also known as an instantiation of a class.

Here's a simple example to understand the definition of a class in Java:


public class Dog{
  String name;
  int age;
  
  void bark(){ //bark
    System.out.println(" Woof, don't come ");
  }
 
  void hungry(){ //hunger
    System.out.println(" Master, I'm hungry ");
  }
}


Description of the example:


public  Is a modifier of a class to indicate that it is a public class and can be accessed by other classes. Modifiers are covered in the next section. 
class  Is the keyword that defines the class. 
Dog  Is the class name. 
name , age  Is a member variable of a class, also called a property; bark() , hungry()  Is a function in a class, also called a method. 

A class can contain the following type variables:

Local variables: variables defined in a method or statement block are called local variables. Variables are declared and initialized in a method, and when the method ends, the variable is automatically destroyed. Member variables: a member variable is a variable defined in a class outside of the method body. This variable is instantiated (allocates memory) when the object is created. Member variables can be accessed by methods in a class and by class-specific statements. Class variables: class variables are also declared outside the body of the method in the class, but must be declared as static. Static is also a type of modifier, as we'll see in the next section.

A constructor

Methods that are executed automatically during class instantiation are called constructors and do not need to be called manually. Constructors can do some initialization during class instantiation.

The constructor must have the same name as the class and no return value.

Each class has a constructor. If a constructor is not explicitly defined for the class, the Java compiler will provide a default constructor for the class.

Here is an example of a constructor:


public class Dog{
  String name;
  int age;
  
  //Constructor with no return value
  Dog(String name1, int age1){
    name = name1;
    age = age1;
    System.out.println(" Thank you for adopting me ");
  }
  
  //Normal methods must have a return value
  void bark(){
    System.out.println(" Woof, don't come ");
  }
 
  void hungry(){
    System.out.println(" Master, I'm hungry ");
  }
  
  public static void main(String arg[]){
    //The parameters passed when the object is created correspond to the constructor parameter list
    Dog myDog = new Dog(" Flower flower ", 3);
  }
}

Operation results:


 Thank you for adopting me 

Description:

The constructor cannot be called by display. The constructor cannot have a return value because there is no variable to receive the return value.

Create an object

An object is an instance of a class, and the process of creating an object is also called instantiation of a class. Objects are created with classes as templates.

In Java, using the new keyword to create an object typically involves three steps:
Declaration: declares an object, including its name and type.
Instantiation: use the keyword new to create an object.
Initialization: when an object is created with new, the constructor is called to initialize the object.

Such as:


Dog myDog; //Declare an object
myDog = new Dog(" Flower flower ", 3); //  instantiation 


Initialization can also be done at the same time as declaration:


Dog myDog = new Dog(" Flower flower ", 3);


Access member variables and methods

Access the member variables and methods through the created objects, for example:


//instantiation
Dog myDog = new Dog(" Flower flower ", 3);
//Member variables are accessed by a dot
myDog.name;
//Access the member method by clicking
myDog.bark();

The following example demonstrates how to access member variables and methods:


public class Dog{
  String name;
  int age;
  
  Dog(String name1, int age1){
    name = name1;
    age = age1;
    System.out.println(" Thank you for adopting me ");
  }
  
  void bark(){
    System.out.println(" Woof, don't come ");
  }
 
  void hungry(){
    System.out.println(" Master, I'm hungry ");
  }
  
  public static void main(String arg[]){
    Dog myDog = new Dog(" Flower flower ", 3);
    //Accessing member variables
    String name = myDog.name;
    int age = myDog.age;
    System.out.println(" I am a little dog, my name is " + name + " , I " + age + " At the age of ");
    //Access methods
    myDog.bark();
    myDog.hungry();
  }
}

Operation results:


 Thank you for adopting me 
 I am a puppy, my name is hua hua, I 3 At the age of 
 Woof, don't come 
 Master, I'm hungry 

The basic order in which Java classes are run
We illustrate the running order of a basic Java class with the following classes:


public class Demo{
  private String name;
  private int age;
  public Demo(){
    name = " The school ";
    age = 3;
  }
  public static void main(String[] args){
    Demo obj = new Demo();
    System.out.println(obj.name + " The age is " + obj.age);
  }
}

The basic running order is:

Run to line 9, which is the entry point to the program. And then I'm going to run to line 10, where I want a new Demo, and I'm going to call the Demo constructor. Run to line 5. Note: many people may think it's time to run line 6. No! To initialize a class, you must first initialize its properties. So run to line 2, and then line 3. After the property is initialized, go back to the constructor and execute the code, lines 6 and 7. Then line 8 indicates that a new Demo instance is complete. Then go back to the main method and execute line 11. And then line 12, the main method is done.

As a programmer, should be clear about the basic running process of the program, otherwise confused, is not conducive to writing code, is not conducive to the development of technology.


Related articles: