Classes must be defined before they can be used. A class is a template for creating an object, and the creation of an object is also called class instantiation.
The following is a simple example to understand the definition of a class in Java:
public class Dog{ String name; int age; void bark(){ // Barking System.out.println("Wangwang, don't come over"); } void hungry(){ // Hungry System.out.println("Master , I'm hungry"); }}
Description of the example:
public is a modifier of the class, indicating that the class is a public class and can be accessed by other classes. The modifier will be explained in the next section. class is the keyword that defines the class. Dog is the class name. name and age are member variables of the class, also called attributes; bark() and hungry() are functions in the class, also called methods.
A class can contain the following type variables:
Construction method
The method that is automatically executed during class instantiation is called a constructor, which does not require you to call it manually. The constructor can do some initialization work during class instantiation.
The name of the constructor must be the same as the name of the class and there is no return value.
Each class has a constructor. If the constructor is not explicitly defined for the class, the Java compiler will provide the class with a default constructor.
Here is an example of a constructor:
public class Dog{ String name; int age; // Construct method, no return value Dog(String name1, int age1){ name = name1; age = age1; System.out.println("Thanks to the owner for adopting me" ); } // Ordinary method must have the return value void bark(){ System.out.println("Wow, don't come over"); } void hungry(){ System.out.println("Master, I'm hungry"); } public static void main(String arg[]){ // The parameters passed when creating an object must correspond to the constructor parameter list Dog myDog = new Dog("Huahua", 3); }}
Running results:
Thanks to my master for adopting me
illustrate:
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 generally has the following three steps:
Declaration: Declare an object, including the object name and object type.
Instantiation: Use the keyword new to create an object.
Initialization: When creating an object with new, the constructor method is called to initialize the object.
For example:
Dog myDog; // Declare an object myDog = new Dog("Huahua", 3); // Instantiate
It can also be initialized while declaring:
Dog myDog = new Dog("Huahua", 3);
Access member variables and methods
Access member variables and member methods through created objects, for example:
// Instantiate Dog myDog = new Dog("Huahua", 3);// Access the member variable myDog.name through point number;// Access the member method myDog.bark() through point number;
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("Thanks to the owner for adopting me"); } void bark( ){ System. out.println("Woom, don't come over"); } void hungry(){ System.out.println("Master, I'm hungry"); } public static void main(String arg[]){ Dog myDog = new Dog ("Huahua", 3); // Access member variable String name = myDog.name; int age = myDog.age; System.out.println("I am a puppy, my name is "+ name + ", I " + age + "year-old"); // Access method myDog.bark(); myDog.hungry(); }}
Running results:
Thanks to the owner for adopting me. I am a puppy. My name is Huahua. I am 3 years old. Don’t come over to the owner. I’m hungry.
Basic running order of Java classes <br />We will explain the running order of a basic Java class in the following class:
public class Demo{ private String name; private int age; public Demo(){ name = "Weixueyuan"; age = 3; } public static void main(String[] args){ Demo ob j = new Demo(); System. out.println(obj.name + "The age is" + obj.age); }}
The basic running order is:
As a programmer, you should be clear about the basic running process of the program, otherwise being confused will be detrimental to writing code and technical development.