2

In java, do we really have to do that in order our variable or method to be public? For example:

void aaa() {
...
}

or

public void aaa() {
...
}

If it is a must, why?

wattostudios
  • 8,666
  • 13
  • 43
  • 57
El3ctr0n1c4
  • 400
  • 2
  • 7
  • 18
  • I'm not sure what your question is. If you want a method to be public, you must declare it public. By default it will have default (package) access. – DNA Feb 22 '12 at 17:06
  • 4
    possible duplicate of [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) – Rob Hruska Feb 22 '12 at 17:08
  • 1
    @RobHruska I think you didn't get my question clearly. Please put in effort for a solution, not to investigate some duplications – El3ctr0n1c4 Feb 22 '12 at 17:22
  • We probably have enough 'solutions' already ;-) – Andy Feb 22 '12 at 17:34

7 Answers7

13

Well that's not a variable, that's a method - but yes, you have to do that to make a method public. If you don't, it has the default package access, and won't be accessible from other packages. You should judge for yourself whether any particular method you write should be accessible only within the same class (make it private) to subclasses (make it protected), to the package (leave the default; you can't state package access explicitly, unfortunately) or to everything (make it public).

(This is a slight simplification of the access modifiers, but it's a start.)

As for why this is the case - typically you should limit visibility so that your type only exposes the methods which really make sense for the concept it's trying to encapsulate. You may have many more private methods which are implementation-specific, and which the outside world shouldn't know or care about.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 2
    @JonSkeet I think people down vote your answer sometimes to check whether Stackoverflow's down vote function works on your answers or not! – Vishwanath Dalvi Feb 22 '12 at 20:04
9

You can ommmit access level keyword

You can ommmit access level modifier anywhere except near main method. There it must be set to public.

Nerijus G
  • 918
  • 13
  • 28
  • Interesting to see, that *no modifier* allows package access level, which I find almost completely useless. What does make sence is *protected* because of the subclass access level, useful for inheritance. I wouldn't know any case where *Class* + *Package* access levels would make any sense. – Socrates Mar 25 '18 at 15:36
5

Not putting a modifier is actually different than public, private, or protected.

With no modifier, only the class itself and any class in the same package can access the attribute.

Learn more here: In Java, difference between default, public, protected, and private

Community
  • 1
  • 1
Cam
  • 14,930
  • 16
  • 77
  • 128
1

The two methods are different: the first has package visibility, while the second one is public.

The difference is that package-visible methods appear public only to methods inside the same package, while public methods are visible to all methods, inside and outside the package.

So the answer to your question depends on your intent: if your method is truly part of your component's interface, make it private; if it is designed for use only inside the package, keep it at the default package visibility.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

In your first example, the method will be package scoped -- i.e. only things in the same package can use it. So yes, you do need to declare public fields/methods/classes as such, otherwise they will have package scope.

Andy
  • 8,870
  • 1
  • 31
  • 39
0

The default is package private.

If you want public or anything else, you must specify it.

Method can be accessed by the class itself, other classes within the same package, but not outside of the package, and not by sub-classes.

See chart at bottom:
http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html

Adrian
  • 5,603
  • 8
  • 53
  • 85
0

I think it is assuming public in Java, so it is not really required.If you start having public, protected, and private throw in the mix in a single class, it is easier to see what scope the method may have.

apaderno
  • 28,547
  • 16
  • 75
  • 90
Churk
  • 4,556
  • 5
  • 22
  • 37