Java as an object-oriented language. Supports the following basic concepts:
•Polymorphic
•inherit
•Packaging
•abstract
•kind
•Object
•Example
•method
•Message parsing
In this section, we focus on the concepts of objects and classes.
•Object: An object is an instance of a class, stateful and behavioral. For example, a dog is an object whose states include: color, name, breed; behaviors include: wagging his tail, calling, eating, etc.
•Class: A class is a template that describes the behavior and state of a class of objects.
Objects in Java
Now let's dig into what an object is. If you look at the real world around you, you will find that there are many objects around you, cars, dogs, people, etc. All of these objects have their own state and behavior.
Take a dog as an example. Its states include: name, breed, color, and behaviors include: yelling, wagging tail and running.
Comparing real objects and software objects, they are very similar.
Software objects also have state and behavior. The state of a software object is attributes, and the behavior is reflected through methods.
In software development, methods operate on changes in the internal state of objects, and mutual calls to objects are also completed through methods.
Classes in Java
Classes can be regarded as templates for creating Java objects.
Use the following simple class to understand the definition of a class in Java:
public class Dog{ String breed; int age; String color; void barking(){ } void hungry(){ } void sleeping(){ } }A class can contain the following type variables:
• Local variables: Variables defined in methods, constructors, or statement blocks are called local variables. Variable declaration and initialization are both in the method. After the method is finished, the variable will be automatically destroyed.
•Member variables: Member variables are variables defined in a class and outside the method body. This variable is instantiated when creating an object. Member variables can be accessed by methods, constructors, and statement blocks in a class.
•Class variables: Class variables are also declared in the class, outside the body of the method, but must be declared as static type.
A class can have multiple methods. In the above example: barking(), hungry() and sleeping() are all methods of Dog class.
Construction method
Each class has a constructor. If the constructor is not explicitly defined for the class, the Java compiler will provide a default constructor for the class.
When creating an object, at least one constructor must be called. The name of the constructor must be the same as the class, and a class can have multiple constructors.
Here is an example of a constructor:
public class Puppy{ public Puppy(){ } public Puppy(String name){ // This constructor has only one parameter: name } }Create an object
Objects are created from classes. In Java, use the keyword new to create a new object. Creating an object requires 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 using new to create an object, the constructor method is called to initialize the object.
Here is an example of creating an object:
public class Puppy{ public Puppy(String name){ //This constructor has only one parameter: name System.out.println("Passed Name is :" + name ); } public static void main(String []args){ // The following statement will create a Puppy object Puppy myPuppy = new Puppy( "tommy" ); } }Compile and run the above program and the following result will be printed:
Passed Name is :tommy
Access instance variables and methods
Access member variables and member methods through created objects as follows:
/* Instantiated object*/ObjectReference = new Constructor(); /* Access the variable*/ObjectReference.variableName; /* Access the method in the class*/ObjectReference.MethodName();The following example shows how to access instance variables and call member methods:
public class Puppy{ int puppyAge; public Puppy(String name){ // This constructor has only one parameter: name System.out.println("Passed Name is :" + name ); } public void setAge( int age ){ puppyAge = age; } public int getAge( ){ System.out.println("Puppy's age is :" + puppyAge ); return puppyAge; } public static void main(String []args){ /* Create an object*/ Puppy myPuppy = new Puppy( "tommy" ); /* Set age by method */ myPuppy.setAge( 2 ); /* Call another method to get age */ myPuppy.getAge( ); /* You can also access member variables like the following */ System.out.println("Variable Value :" + myPuppy.puppyAge ); } }Compile and run the above program, and produce the following results:
Passed Name is :tommy
Puppy's age is :2
Variable Value:2
Source file declaration rules
In the last part of this section, we will learn about the declaration rules of the source files. Pay special attention to these rules when defining multiple classes in a source file, and also having import statements and package statements.
• There can only be one public class in a source file
•A source file can have multiple non-public classes
•The name of the source file should be consistent with the class name of the public class. For example: the class name of the public class in the source file is Employee, so the source file should be named Employee.java.
•If a class is defined in a package, the package statement should be on the first line of the source file.
•If the source file contains an import statement, it should be placed between the package statement and the class definition. If there is no package statement, the import statement should be at the front of the source file.
•The import statement and package statement are valid for all classes defined in the source file. In the same source file, different package declarations cannot be given to different classes.
Classes have several access levels, and classes are also divided into different types: abstract classes and final classes, etc. These will be described in the Access Control section.
In addition to the types mentioned above, Java also has some special classes, such as internal classes and anonymous classes.
Java package
Packages are mainly used to classify classes and interfaces. When developing Java programs, hundreds of classes may be written, so it is necessary to classify classes and interfaces.
Import statement
In Java, if a complete qualified name is given, including package name and class name, then the Java compiler can easily locate the source code or class. The Import statement is used to provide a reasonable path so that the compiler can find a certain class.
For example, the following command line will command the compiler to load all classes under the java_installation/java/io path
import java.io.*;
A simple example
In this example, we create two classes: Employee and EmployeeTest.
First, open the text editor and paste the following code in. Note that you save the file as Employee.java.
The Employee class has four member variables: name, age, designation, and salary. This class explicitly declares a constructor that has only one parameter.
import java.io.*; public class Employee{ String name; int age; String designation; double salary; // Constructor of Employee class public Employee(String name){ this.name = name; } // Set the value of age public void empAge(int empAge){ age = empAge; } /* Set the value of designation*/ public void empDesignation(String empDesig){ designation = empDesig; } /* Set the value of salary*/ public void empSalary(double empSalary){ salary = empSalary; } /* Print information*/ public void printEmployee(){ System.out.println("Name:"+ name); System.out.println("Age:" + age); System.out.println("Designation:" + designation); System.out.println("Salary:" + salary); } }The program starts with the main method. In order to run this program, you must include the main method and create an instance object.
Below is the EmployeeTest class, which instantiates 2 instances of Employee class and calls the method to set the value of the variable.
Save the following code in the EmployeeTest.java file.
import java.io.*; public class EmployeeTest{ public static void main(String args[]){ /* Create two objects using the constructor*/ Employee empOne = new Employee("James Smith"); Employee empTwo = new Employee("Mary Anne"); // Call the member method of these two objects empOne.empAge(26); empOne.empDesignation("Senior Software Engineer"); empOne.empSalary(1000); empOne.printEmployee(); empTwo.empAge(21); empTwo.empDesignation("Software Engineer"); empTwo.empSalary(500); empTwo.printEmployee(); } }Compile these two files and run the EmployeeTest class, you can see the following results:
C :> javac Employee.java C :> vi EmployeeTest.java C :> javac EmployeeTest.java C :> java EmployeeTest Name:James Smith Age:26Designation:Senior Software Engineer Salary:1000.0Name:Mary Anne Age:21Designation:Software Engineer Salary:500.0
The above article has a deep understanding of Java objects and classes. It is all the content I have shared with you. I hope it can give you a reference and I hope you can support Wulin.com more.