005 The Object in JAVA, 005 object
As we all know, JAVA is an object-oriented development language. In JAVA, everything is carried by classes and objects. The class is an abstract generalization (for example: person, animal, book, etc.), and the object is constructed by the class (for example: Person p = new Person (), here p is an object), the object holds the data status, but the class does not, we can regard the class as something similar to the incubator, when we need to use the class to carry data or use methods in the class, we can use the class we want to use to construct the desired object, and then use this object. The data status held by an object is usually a variable, which is called a member variable. Member variables are stored together with objects.
Let's take a look at the instance:
1 class Point {2 int x;3 int y;4 }
Class Point is a Class we define. (Note: The class only serves as the definition and does not allocate space. The class can be seen as the raw materials and steps required for constructing a thing)
We can use the following statement to construct the instance (object) of this Point class)
Point p = new Point (); // here, we use the default constructor of Class Point ()
// Note: if we do not define our own constructor, we will use the default constructor of the class.
In fact, we can define our constructors for use when creating objects, for example:
Class Point {int x; int y; Point () {// This is the default class. If we do not have any constructor, the compiler adds this} Point (int x, int y) {this. x = x; this. y = y;} public static void main (String [] args) {Point p = new Point (2, 3 ); // We can use the constructor we defined to construct the Point instance p as follows. System. out. println ("the value of member variable x in object p is:" + p. x); // we can use p. x Access Object p member variable x System. out. println ("the value of member variable y in object p is:" + p. y); // we can use p. y: member variable of object p accessed by y }}