Repaso
Español
Java CoreCoreJV-08

Which access modifier is used when you write NO keyword in front of a field or method?

Options

  1. public: visible to every class in the project
  2. private: visible only inside the same class
  3. Package-private: visible only to classes in the same package (Correct option)
  4. protected: visible to subclasses and the same package

Answer

Package-private: visible only to classes in the same package

Why

Java has four levels of visibility:

  • public: everyone.
  • protected: same package plus subclasses.
  • (no keyword) package-private: only classes in the same package.
  • private: only inside the same class.

Good practice (encapsulation): make fields private and expose only what is needed through public methods.

Remember

No keyword = package-private.

Key terms

Practice this question in Repaso