Java objects and classes
Before introducing Java classes and objects, let's take a brief look at Object-Oriented Programming (OOP). Programming is the design of programs through classes and objects, the object represents an entity and the entity can be clearly identified.
Java is an object-oriented language. The following basic concepts are supported:
- Class
- Object
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
- Abstract Class
- Interface
- Object: An object is an instance of a class that has a state and behavior. For example, a cat is an object whose states are: color, name, and breed; behaviors are: tail wagging, eating, etc.
- Class: A class is a template that describes the behavior and state of a class of objects.
Now let's see what's an object. If you look at the real world around you, you will find many objects, cats, cars, dogs, people, etc. around you. All these objects have their own state and behavior.
Software objects also have states and behavior. The state of the software object is the property, and the behavior is reflected by the method.
In software development, methods operate on changes in the internal state of objects, and mutual calls between objects are also accomplished through methods.
public class Cat { int age; String color; void hungry () { } void sleeping () { } }
Java has three kinds of variables:
- Local variables: Variables defined in a method, constructor or block of statements are called local variables. Variable declaration and initialization are in the method, after the method ends, the variable will be automatically destroyed.
- Instance variables: Instance variables are variables defined in the class, outside the method body. This variable is instantiated when the object is created. Instance variables can be accessed by class methods, constructors, and class-specific blocks and destroyed when the object is destroyed.
- Class variables: Class variables are also declared in the class, outside the method body, but must be declared as static type. Static variables are created when the program starts and destroyed when the program ends.
A class can have multiple methods, in the above example: hungry() and sleeping() are two methods of the Cat class.
Java Constructor
Each of the Java classes has a constructor. If no constructor is explicitly defined for the class, the Java compiler will provide a default constructor for the Java class.
When creating an object, at least one constructor must be called. The name of the constructor must have the same name as the class name, and a class can have multiple constructors.
Here is an example of a constructor:
public class Cat { public Cat () { } public Cat (String CatName) { // This constructor has only one parameter: CatName } }
Create 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: Declares an object, including the object name and object type.
- Instantiate: Use the keyword new to create an object.
- Initialization: When an object is new created , the constructor is called to initialize the object.
Here is an example to illustrate how to create an object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class Cat { // constructor // only one parameter public Cat(String cName) { System.out.println("The cat's name is " + cName); } public static void main(String[] args) { // create a Cat object Cat cat = new Cat("Leo"); } } |
The cat's name is Leo