class must be defined before it can be used. A class is a template for creating an object, which is also called an instantiation of the class.
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 ("Barking, don't Come");
}
void hungry () {//Hunger
System.out.println ("host, I am Hungry");
}
Description of the example:
Public is a modifier of a class that indicates that the class is a common class and can be accessed by other classes. Modifiers are explained in the next section. Class
is the keyword that defines the classes.
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 types of variables:
- Local variables: Variables defined in a method or statement block are called local variables. Variable declarations and initializations are in the method, and when the method is finished, the variable is automatically destroyed.
- Member variables: member variables are variables defined in the class, outside the method body. This variable is instantiated (allocating memory) when the object is created. Member variables can be accessed by the methods in the class and by the statements of a particular class.
- Class variables: Class variables are also declared in the class, outside the method body, but must be declared as a static type. Static is also a modifier, which is explained in the next section.
Construction method
The method that is executed automatically during class instantiation is called the construction method, which does not require you to call it manually. A construction method can do some initialization work in the process of instantiating a class.
The name of the constructor must be the same as the class name, and there is no return value.
Each class has a construction method. If you do not explicitly define a construction method for a class, the Java compiler will provide a default constructor for the class.
The following is an example of a construction method:
public class dog{
String name;
int age;
Construction method, no return value
Dog (String name1, int age1) {
name = name1;
age = Age1;
SYSTEM.OUT.PRINTLN ("Thank the host for adopting Me");
}
Normal method, must have return value
void bark () {
System.out.println ("Bark, don't Come");
}
void Hungry () {
System.out.println ("Master, I Am Hungry");
}
public static void Main (String arg[]) {
//parameter to be passed when creating an object corresponds to the Construction method argument list
Dog Mydog = new Dog ("Flowers", 3);
}
}
Run Result:
Thank you, Master, for adopting me.
Description
- The constructor method cannot be displayed for invocation.
- The constructor method cannot have a return value because there is no variable to receive the return value.
Creating objects
An object is an instance of a class, and the process of creating an object is also called an instantiation of the class. Objects are created with a class as a template.
In Java, you use the New keyword to create an object, which typically has the following three steps:
Declaration: Declares an object, including the object name and object type.
Instantiation: Use the keyword new to create an object.
Initialization: When you create an object using new, the constructor initialization object is called.
For example:
Dog Mydog; Declares an object
Mydog = new Dog ("Flower", 3);//instantiation
It can also be initialized at the same time as the declaration:
Dog Mydog = new Dog ("Floral", 3);
Accessing member variables and methods
Access member variables and member methods through the objects you have created, for example:
Instantiate
Dog Mydog = new Dog ("Flower", 3);
Accessing member variable mydog.name by dot number
;
Access member method via dot number
mydog.bark ();
The following example shows 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 the host for adopting Me");
}
void Bark () {
System.out.println ("Bark, don't Come");
}
void Hungry () {
System.out.println ("Master, I Am Hungry");
}
public static void Main (String arg[]) {
Dog Mydog = new Dog ("Floral", 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 + "years old");
Access Method
Mydog.bark ();
Mydog.hungry ();
}
Run Result:
Thank you for adopting me.
I'm a puppy, my name is flower, I'm 3 years old.
bark, don't come over
, master, I'm hungry.
Basic running order of Java classes
We use the following class to illustrate the order in which a basic Java class is run:
public class demo{
private String name;
private int age;
Public Demo () {
name = "Micro-learning court";
Age = 3;
}
public static void Main (string[] args) {
Demo obj = new demo ();
System.out.println (Obj.name + "age is" + obj.age);
}
The basic order of operations is:
- First run to line 9th, which is the entry for the program.
- Then run to line 10th, where you want to new a demo, you need to invoke the construction method of the demo.
- Run To line 5th, note: Many people may feel that the next should run the 6th line, wrong! Initializes a class that must first initialize its properties.
- So run to line 2nd, then line 3rd.
- After the property is initialized, the constructor is returned to the construction method, executing the code inside, that is, line 6th, Row 7th.
- Then the 8th line indicates that the new demo instance is complete.
- Then go back to the main method to perform line 11th.
- Then line 12th, the main method is finished.
As a programmer, you should be aware of the basic procedures of the operation, otherwise confused, not conducive to writing code, and not conducive to the development of technology.