Access Modifiers in Methods

Access modifiers control the visibility of methods within classes and across different packages. Common access modifiers are:

  • public: The method is accessible from any other class.
  • private: The method is accessible only within its own class.
  • protected: The method is accessible within its own package or subclasses.
  • default (no modifier): The method is accessible within its own package.
Code Example
Output
This is a private method.