0

In IDE Some public methods are marked with a warning as Access can be package local and if I remove public form the method no warning will be shown. Is it a good practice to do so? Should I keep them public anyways ?

A Sdi
  • 665
  • 2
  • 10
  • 24
  • The general rule of thumb is to only make something public if you want another class from another package to be able to access it. – billoot Nov 09 '16 at 16:20
  • You shouldn't always do one or the other. Rather, consider the interface of your component (i.e. potentially multiple classes). What operations are you exposing to consumers? Those should be public. – Taylor Nov 09 '16 at 16:21

1 Answers1

1

If the method is only accessed within the package, the best practice is to use the "default" access modifier (by not specifying any access modifier). Further more briefly here are the four access modifiers used in Java and there accessibility levels.

  • default- Visible to the package. No modifiers are needed.
  • private- Visible to the class only.
  • public- Visible to the world.
  • protected- Visible to the package and all subclasses.
dammina
  • 655
  • 1
  • 8
  • 23