class must be defined before it can be used. The class is the template that creates the object, and the object that is created is also called the instantiation of the 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("bark, don't come Over");
- }
- void hungry(){ //hunger
- System. Out. println("Master, I'm 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 a keyword that defines a class.
- Dog is the class name.
- Name, age is a member variable of a class, also called an attribute; 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 block of statements are called local variables. Both the Declaration and initialization of variables are in the method, and the variables are automatically destroyed when the method ends.
- Member variables: Member variables are variables that are 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 methods in the class and statements of a particular class.
- Class variables: Class variables are also declared in the class, outside the method body, but must be declared as static types. Static is also a modifier, which is explained in the next section.
Construction method
A method that is automatically executed during a class instantiation is called a construction method, and it does not require you to invoke it manually. The construction method can do some initialization work during the instantiation of the class.
The name of the construction method must be the same as the name of the class, and there is no return value.
Each class has a constructor method. If you do not explicitly define a construction method for a class, the Java compiler will provide a default constructor for that class.
Here is an example of a construction method:
- Public class Dog{
- String name;
- int age;
- //constructor method, no return value
- Dog(String name1, int age1){
- Name = name1;
- Age = Age1;
- System. Out. println("Thank the Master for adopting Me");
- }
- //Normal method, must have a return value
- void Bark(){
- System. Out. println("bark, don't come Over");
- }
- void hungry(){
- System. Out. println("Master, I'm Hungry");
- }
- public static void main(String arg[]){
- //The arguments passed when creating the object should correspond to the constructor method parameter list
- dog Mydog = new dog("Flower", 3);
- }
- }
Operation 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, using the new keyword to create an object 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.
- Initialize: When you create an object with new, the constructor method is called to initialize the object.
For example:
- Dog Mydog; //Declare an object
- Mydog = new Dog("Flower", 3); //instantiation
It can also be initialized at the same time as the declaration:
- Dog Mydog = new dog("Flower", 3);
Accessing member variables and methods
Access member variables and member methods through the objects you have created, such as:
- Instantiation of
- Dog Mydog = new dog("Flower", 3);
- Accessing member variables by point number
- Mydog. Name;
- Accessing member methods by point 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 Master for adopting Me");
- }
- void Bark(){
- System. Out. println("bark, don't come Over");
- }
- void hungry(){
- System. Out. println("Master, I'm Hungry");
- }
- public static void main(String arg[]){
- dog Mydog = new dog("Flower", 3);
- //Access member variables
- String Name = Mydog. Name;
- int Age = Mydog. Age;
- System. Out. println("I am a puppy, my name is" + name + ", i" + age + "old");
- //Access Method
- Mydog. Bark();
- Mydog. Hungry();
- }
- }
Operation Result:
Thank you, Master, 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.
Definition and instantiation of the 1.Java class