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:
1 Public classdog{2 String name;3 intAge ;4 5 voidBark () {//Barking6System.out.println ("Bark, don't come over");7 }8 9 voidHungry () {//HungerTenSystem.out.println ("Master, I'm hungry.")); One } A}
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:
1 Public classdog{2 String name;3 intAge ;4 5 //constructor method, no return value6Dog (String name1,intage1) {7Name =name1;8Age =Age1;9SYSTEM.OUT.PRINTLN ("Thank the Master for adopting me");Ten } One A //normal method, must have a return value - voidbark () { -System.out.println ("Bark, don't come over"); the } - - voidhungry () { -System.out.println ("Master, I'm hungry.")); + } - + Public Static voidMain (String arg[]) { A //parameters passed when creating an object correspond to the constructor method parameter list atDog Mydog =NewDog ("Floral", 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:
1 // declaring an object 2 New // instantiation of
It can also be initialized at the same time as the declaration:
1 New Dog ("Floral", 3);
Accessing member variables and methods
Access member variables and member methods through the objects you have created, such as:
1 // instantiation of 2 New Dog ("Floral", 3); 3 // accessing member variables by point number 4 Mydog.name; 5 // accessing member methods by point number 6 Mydog.bark ();
The following example shows how to access member variables and methods:
1 Public classdog{2 String name;3 intAge ;4 5Dog (String name1,intage1) {6Name =name1;7Age =Age1;8SYSTEM.OUT.PRINTLN ("Thank the Master for adopting me");9 }Ten One voidbark () { ASystem.out.println ("Bark, don't come over"); - } - the voidhungry () { -System.out.println ("Master, I'm hungry.")); - } - + Public Static voidMain (String arg[]) { -Dog Mydog =NewDog ("Floral", 3); + //Accessing member variables AString name =Mydog.name; at intAge =Mydog.age; -System.out.println ("I am a puppy, my name is" + name + ", I" + Age + "old")); - //access Method - Mydog.bark (); - mydog.hungry (); - } in}
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.
Series Articles:
Java know how much (1) Language overview
Java know how much (2) virtual machine (JVM) and cross-platform principle
Java know how much (3) employment direction
Java know how much (4) the difference between J2SE, Java EE, J2ME
Java know how much (5) Build Java development environment
Java know how much (6) The first example of a program
Java knows how many (7) classes and objects
Java know how much (8) class library and its organizational structure
Java know how much (9) Import and Java class search path
Java know how much (10) data types and variables
Java know how much (11) data type conversions
Java know how many (12) operators
Java know how much (13) Process Control
Java know how many (14) arrays
Java know how much (15) string
Java know how much (StringBuffer) and Stringbuider
Java know how much (17) emphasize the programming style
Java know how many (18) classes are defined and instantiated
Java know how many (18) classes are defined and instantiated