Which access modifier is used when you write NO keyword in front of a field or method?
Options
public: visible to every class in the projectprivate: visible only inside the same class- Package-private: visible only to classes in the same package (Correct option)
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.