005 the Object in JAVA

Source: Internet
Author: User

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

Related Article

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.