Что значит private в java?

Пользователь

от jerad.kuphal , в категории: Java , 7 месяцев назад

Что значит private в java?

Facebook Vk Ok Twitter LinkedIn Telegram Whatsapp

1 ответ

Пользователь

от brook , 3 месяца назад

@jerad.kuphal 

In Java, the private keyword is an access modifier that is used to restrict access to the members (fields, methods, and nested classes) of a class. When a member is declared as private, it can only be accessed within the class in which it is declared. It is not accessible from any other class, even if that class is a subclass of the class in which the private member is declared.


Here is an example of how the private keyword is used in a Java class:

1
2
3
4
5
6
public class MyClass {
   private int myField;
   private void myMethod() {
      // method implementation
   }
}


In this example, the myField field and the myMethod method are both declared as private. This means that they can only be accessed within the MyClass class, and not from any other class.


It is important to use the private keyword appropriately in order to properly encapsulate the members of a class and protect their values from being modified by external code.