I have been working with android for a few years now, not once have I had a teacher or anyone to tell me what to do.
This whole time I have wondered to myself this.
When you have a method I generally see…
public void method(){
//Stuff
}
or
private void method(){
//stuff
}
I know that a void is a method with no return value, and that public is the visibility of the method in a way but would it matter if I just used something like this…
void method(){
//stuff
}
Because then the methods visibility would just be default anyway?
I have no idea if I am right or not, is it just good practice to specify “public” or “private” ?
Not specifying anything has a specific meaning:
public– any class can access this memberprotected– subclasses can access this member (as well as code in the same class or in the same package)private– only code in the same class can access this memberArguably the last case should have had its own keyword, but we’re stuck with it now. Unless you really mean to use default visibility, it’s poor form to not specify anything – did you really need package visibility for some reason, or did you just default to package visibility for everything? Best practice is to explicitly use
privatefor non-public members unless you need one of the others.