What is the use of private in Java

Source: Internet
Author: User

This is a very old question, and I remember explaining to my classmates that I was not very clear about this concept when I was getting started on the object-oriented approach.
Do not know the landlord on the structure has no concept? (provided on this leave) (Of course, the intention of my writing is to discuss with the landlord.
Also discussed with other friends)
1. Write a structure, give it a name, define several data within it
2. Then create an instance of it
3. You can use the name of an instance that you created earlier. The name of a data in a structure can be obtained by such a way
or change the data.
This third step works in C or VB, but it doesn't work in Java.
In Java
public class Classone
{
private int dataone;
Private String Datatwo;
...
Private custom type Datan;
}
Classone obj=new Classone ();
obj.dataone=5;
Specifically, that's not going to work.
The way the object-point data is used in Java is not workable
Because the data is private, that is, the front of the data plus private, of course, is not defined as private, but is generally private (the reason behind that)
So you can't access the data through the object point.
Now, why do you want to define data as private?
Defining data as private through public methods to access data is a specification
We have to define the data as private because we have to comply with the specification.
I used to play. NET, and then play Java, a big feeling is that Java to. NET to have a specification
A thing, very inadvertently in. NET, in Java, there is a special name, in. NET can be fabricated, completely do not speak the specification (although not advocated, but it is possible)
In the Java Central Africa but it's hard to even say no. I am very deeply touched by this.
So programming in Java is about specification. In addition to employing enterprises are afraid to speak the norms ....
So let's go along with the specs, starting with the first one in front of the data and the public way to access the data!!!!

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.