The Private keyword means that no one can access that member unless that particular class, and from the method of that class. Other members within the same package cannot access private members, which makes it seem as if the class is isolated from ourselves. On the other hand, it is not possible to create a package by several cooperating people. So private allows us to change that member freely, without caring whether it affects another class within the same package. The default "friendly" package access is usually an appropriate way to hide, and keep in mind that you cannot access a "friendly" Member for a package user. This effect is often satisfying, because default access is the method we usually use. For members who want to become public (publicly), we usually make it clear that they can be invoked freely by the client programmer. And as a result, it's often the first time that you don't have to use the Private keyword frequently, because you can simply publish your code without it (this is a stark contrast to C + +). However, with the depth of study, you will find that private still has a very important use, especially when it involves multithreading (see Chapter 14th for details).
Here's an example of a private application:
: Icecream.java
//demonstrates "private" keyword
class Sundae {
private sundae () {}
static sundae Makeasundae () {return
new Sundae ();
}
}
public class Icecream {public
static void Main (string[] args) {
//! Sundae x = new Sundae ();
Sundae x = Sundae.makeasundae ();
}
} ///:~
This example demonstrates the convenience of using private: Sometimes you might want to control how objects are created and prevent someone from directly accessing a particular builder (or all the builders). In the above example, we cannot create a Sundae object from its builder; instead, we must invoke the Makeasundae () method to implement (Comment ③).
③: There is another effect at this point: because the default builder is uniquely defined and its properties are private, you can prevent inheritance of this class (this is the topic that is highlighted in chapter 6th).
If you decide that a class has only one "helper" method, then for any method, you can set it private so that you don't mistakenly use it elsewhere in the package to prevent you from changing or deleting the method. When you set the property of a method to private, you can guarantee that you keep this option (however, if a handle is set to private, it does not mean that the other object cannot have a public handle to the same object). Questions about "Aliases" will be detailed in chapter 12th.