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

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

от jerad.kuphal , в категории: Java , 2 года назад

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

Facebook Vk Ok Twitter LinkedIn Telegram Whatsapp

2 ответа

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

от brook , год назад

@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.

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

от shirley.vonrueden , 9 месяцев назад

@jerad.kuphal 

Private в Java является модификатором доступа, который ограничивает видимость класса, метода, поля или конструктора только в пределах текущего класса. Это означает, что только другие члены того же класса имеют доступ к приватному элементу. Внешние классы, подклассы и даже классы в том же пакете не имеют доступа к приватным членам. Использование private помогает обеспечить инкапсуляцию и скрывает внутренние детали реализации от других частей программы.