The role of the Java constructor

Source: Internet
Author: User

I always have to confuse the constructor with the method, and later find that

method, in fact, is needed to execute Java code, and the constructor,

Constructor,,, is an instance of a Class!!

Why is it?

class, we need to use the class to create the object to access its properties, because the instance needs to be invoked, but the invocation, we have to consider a problem, that is, the object is ultimately stored in memory, and when stored, our memory will need to give him another new memory space, then , how does Java open up memory space for the classes we need? This involves the Java memory mechanism, that is, we have to make a constructor for this class, and the name of the constructor must be the same as the name of the class, so that our Java compiler can recognize this class, and then give this class in memory to open up memory space, which is what we say, We manually and artificially gave him " initialization ", as in the following example:

Class Rock {

Rock () {

System.out.print ("Rock");

}

}

Thus, when we call the Rock class, our Java compiler will initialize him "automatically" in advance to open up memory space

The role of the Java constructor

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.