005 The Object in JAVA, 005 object

Source: Internet
Author: User

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 }}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.