"Access can be packetLocal" - JAVA

2

Most of the methods that I instantiate in several classes ask me to change it to "packetLocal". Example:

  

public void example ();

go to

  

void example ();

So, should the methods be public or not?

    
asked by SinNombre SinApellido 19.09.2016 в 12:31
source

2 answers

1

In this case surely the access to your method is done only within the package , for this reason the best practice is to use the default access modifier and it is not necessary to add a modifier of access . It should not be public because it would be visible to everyone, it would be accessed from any class or instance that in your case is not necessary.

Access Modifiers :

  • public : visible to everyone.

  • protected : visible within the package and all subclasses.

  • private : visible only to the class that contains it.

  • default : in case this level of accessibility is visible within the package and no modifiers are required.

Here is a good article for more information:

Public, protected, default and private access modifiers in Java.

    
answered by 26.01.2017 / 01:03
source
0

This depends on how you are using the methods if you leave it with public other classes will have access to the method, if you remove the public it will become private and you will only have access to the method from your same class.

    
answered by 19.09.2016 в 16:02