Java private关键字
Java private关键字是访问修饰符。可以将其分配给变量,方法和内部类。这是访问修饰符的最受限制的类型。
Java private要记住的要点
专用访问修饰符只能在同一类中访问。
我们不能将private分配给外部类和接口。
private关键字的最佳用法是通过使该类的所有数据成员变为私有来在Java中创建一个完全封装的类。
如果我们将任何类构造函数设为私有,则不能从类外部创建该类的实例。
如果我们要覆盖任何方法,则被覆盖的方法(即在子类中声明的方法)必须没有更多限制。
根据上一点,如果我们将私有修饰符分配给任何方法或变量,则可以使用所有类型的访问修饰符将该方法或变量覆盖为子类。但是,仍然不能在类外部调用私有方法。
私有关键字的示例
示例1
让我们看一个确定private变量是否可访问的示例。
class A
{
private String msg="try to access the private variable outside the class";
}
public class PrivateExample1 {
public static void main(String[] args) {
A a=new A();
System.out.println(a.msg);
}
}
输出:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The field A.msg is not visible
示例2
让我们看一个确定private方法是否可在类外部访问的示例。
class A
{
private void display()
{
System.out.println("try to access the private method outside the class ");
}
}
public class PrivateExample2 {
public static void main(String[] args) {
A a=new A();
a.display();
}
}
输出:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method display() from the type A is not visible
示例3
让我们看一个示例,以确定是否可以将private修饰符分配给外部类。
private class PrivateExample3 {
void display()
{
System.out.println("try to access outer private class");
}
public static void main(String[] args) {
PrivateExample3 p=new PrivateExample3();
p.display();
}
}
输出:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
示例4
在上面的示例中,我们了解到private方法不能在类外部调用。在这里,我们通过更改类的运行时行为从类外部调用私有方法。
import java.lang.reflect.Method;
class A {
private void display()
{
System.out.println("private method is invoked");
}
}
public class PrivateExample4{
public static void main(String[] args)throws Exception{
class c = Class.forName("A");
Object o= c.newInstance();
Method m =c.getDeclaredMethod("display", null);
m.setAccessible(true);
m.invoke(o, null);
}
}
输出:
private method is invoked
示例5
让我们看一个示例,以确定我们是否在类外部创建私有构造函数的实例。
class A
{
String msg;
private A(String msg)
{
this.msg=msg;
}
void display()
{
System.out.println(msg);
}
}
public class PrivateExample5 {
public static void main(String[] args) {
A a=new A("try to create the instance of private constructor outside the class");
a.display();
}
}
输出:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The constructor A(String) is not visible
示例6
在此示例中,使用默认访问修饰符将private方法重写为子类。但是,仍然不允许我们从子类调用父类方法。
class A{
private void msg()
{
System.out.println("parent class method");
}
}
public class PrivateExample6 extends A{
void msg()
{
System.out.println("child class method");
}
public static void main(String args[]){
PrivateExample6 obj=new PrivateExample6();
obj.msg();
}
}
输出:
示例7
借助示例,我们来看看private关键字的实际用法。
class Employee{
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class PrivateExample7{
public static void main(String args[]){
Employee e=new Employee();
e.setId(101);
e.setName("John William");
e.setAge(25);
System.out.println(e.getId()+" "+e.getName()+" "+e.getAge());
}
}
输出: