Omitting public modifier in java methods [duplicate]
Asked Answered
B

1

7

I am learning Java and there's something bothering me and the textbook doesn't explain it.

I understand that you use modifiers to declare methods inside classes and all. But I suddenly got to a class declared like

static void(){
}

Why is there no public or private modifier and it still works? Can I avoid using the public modifier everywhere else or how does that work? I understand that static means member of the class and void that it doesn't return a value. Yet why not public or private for that matter.

Biomass answered 8/9, 2015 at 0:45 Comment(4)
Looks like this has been addressed pretty thoroughly @ In Java, what's the difference between public, default, protected, and private?Iberian
See java doc docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.htmlTiga
When you see no modifier in front of a method or variable in a class, it means that the access level defaults to something called package private. This is more restricted than protected, but less restricted than private. Here is a link to the official Oracle chart showing this.Taligrade
If no access modifier is provided, the method gets the default visibility which means it's visible within the package. See this chart for a beginner friendly comparison. Look specifically at the int i row! Hope that helps!Phyto
T
7

For the sake of this explanation, the terms "functions" and "methods" are used interchangably. There is a small difference between them, for more information, ask Google.

Methods in Java that do not explicitly specify a modifier are by default package-private, so the method is visible to all the classes in the same package as the class where the method is declared.

Public functions are callable by all classes that have access to the class (i.e your whole project) and private methods are only callable within the class the method was written in. There is also the protected modifier, which specifies that the functions can only be accessed by the class, all its subclasses and classes in the same package.

"Why is that important?", you may ask. Good question!

You should use modifiers to hide methods/properties from other classes which may (ab)use them or in a bad case could lead to unexpected behaviour (not necessarily technically, but semantically... some methods just need a little more privacy just like we do). So a good place to start is private, which means only the class it is declared in is able to call it. More often than not, you'll need to give other classes access to methods, which is why the package-private, protected and public modifiers exist.

Data encapsulation is an important paradigm in programming, and these modifiers help you achieve just that.

Theocentric answered 8/9, 2015 at 0:58 Comment(1)
I am still not able to differentiate between 'Public' and 'Protected'. Functions or Variables having modifier as 'Public' or 'Protected' can be accessed if Parent class say 'A1' is extended in other classes (within and outside the package). So what extra do 'Public' modifier has?? Please explain.Peddling

© 2022 - 2024 — McMap. All rights reserved.