As we all know, Java is an object-oriented development language. In Java, everything is hosted by classes and objects. Where the class is an abstract generalization (Eg:person, animal, book, and so on), and the object is constructed by the class (Eg:person p =new person (), where P is an object), the object holds the data state, and the class does not, We can think of classes as things like incubators, and when we need to use classes to host data or use methods in classes, we can use the class we want to construct the object that I want, and then use that object. The data state held by an object is usually a variable, which we call a member variable. Member variables are the same as objects that are saved together.
OK, let's take a look at the example:
1 CLass Point {2 int x; 3 int y; 4 }
Class point is a class that we define. (Note: Classes are only defined and do not allocate space, and classes can be thought of as the raw material and checklist of steps we need to construct one thing.)
We can construct an instance of the point class (that is, an object) with the following statement
New Point (); Here, we are using the default constructor point () of Class point
Note: If we do not define our own constructors, we will use the class default constructor
In fact, we can define our own constructors for use when creating objects, for example:
classPoint {intx; inty; Point () {//This is the class default, and if we don't have any constructors, the compiler defaults to the class plus this} point (intXinty) { This. x =x; This. y =y; } Public Static voidMain (string[] args) {point P=NewPoint (2, 3);//we can use our defined constructors to construct an instance of point P like this. System.out.println (the value of the member variable x in the object P is: "+ p.x);//we can use p.x to access the member variable x of the object PSystem.out.println ("The value of member variable y in Object P is:" + p.y);//we can use P.Y to access the member variable y of the object P }}
005 the Object in JAVA