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.

peterh
  • 11,875
  • 18
  • 85
  • 108
mateo_io
  • 398
  • 2
  • 3
  • 12
  • 2
    Looks like this has been addressed pretty thoroughly @ [In Java, what's the difference between public, default, protected, and private?](http://stackoverflow.com/questions/215497/in-java-whats-the-difference-between-public-default-protected-and-private) – CollinD Sep 08 '15 at 00:48
  • See java doc https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html – Bacteria Sep 08 '15 at 00:49
  • 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](https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html) to the official Oracle chart showing this. – Tim Biegeleisen Sep 08 '15 at 00:54
  • If no access modifier is provided, the method gets the *default visibility* which means it's visible within the package. See [this chart](http://stackoverflow.com/a/33627846/276052) for a beginner friendly comparison. Look specifically at the `int i` row! Hope that helps! – aioobe Jun 10 '16 at 17:09

1 Answers1

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.

the_critic
  • 12,720
  • 19
  • 67
  • 115
  • 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. – Deepak Apr 26 '18 at 09:55