About access to Java Private member variables

Source: Internet
Author: User
Java main class M can directly access its own private members, even if the new one in main itself, is only a local variable in its own static method, the local variable can still access its own private members, as long as the local variable body in its own class M.

The other Class A is defined outside the main class M, and the following statement is written in the main method of the Master class:

1, M m = new M, the main class private member can be accessed directly by M.P because the statement is in the M class body

2, a A = new A, the private member cannot be accessed directly through A.P, because the statement is in the M class body and is not in the class A body

The inertia of learning C + + in college is that private members can never be accessed directly by new variables, must be encapsulated in the class body with public get ()/set (), and then new variables can only be accessed through get ()/set (). What was not accepted and understood at the time was that the main method was in some kind of body and could also own new. Now it's finally understood.

About access to Java Private member variables

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.