Class is the most important data type in object-oriented languages. You can use classes to declare variables. In object-oriented languages, variables declared with classes are called objects. Different from basic data types, after declaring an object with a class, you must also create the object, that is, assign the variables owned by the declared object and determine the attributes of the object. When using a class to create an object, it is also called giving an instance of this class. In layman's terms, a class is a template for creating objects. Without a class, there would be no object. Creating an object consists of two steps: object declaration and assigning variables to the object.
1. The general format of object declaration is:
The moniker name of the class;
For example:
Laderlader;
2. Assign variables to the declared object:
Use the new operator and the constructor method of the class to assign variables to the declared object, that is, create the object. If there is no constructor in the class, the system will call the default constructor, which has no parameters and no statements in the method body.
For example:
classPoint{intx,y;Point(inta,intb){x=a;y=b;}}publicclassMain{publicstaticvoidmain(String[]args){Pointp1,p2;//Declare objects p1 and p2p1=newPoint(10,10 );//Assign variables to the object (use new and the constructor in the class) p2=newPoint(23,45);//Assign variables to the object (use new and the constructor in the class)}}