tags:

views:

1844

answers:

4

It's a crazy question because it seems simple. I cannot find anywhere that blatantly states the answer.

If I type:

     void doThis(){
        System.out.println("Hello Stackoverflow.");
     }

what is the default scope of doThis()?

Public? Protected? Private?

Thanks in advance.

+3  A: 

The default scope is "default". It's weird--see these references for more info.

Michael Haren
Actually it's not: it's package-private
Maurice Perry
THanks Maurice, I updated my answer.
Michael Haren
I can't delete my answer as it's been accepted. I encourage Joe to go with @Esko's answer!
Michael Haren
+19  A: 

The default scope is package-private. All classes in the same package can access the method/field/class. Package-private is stricter than protected and public scopes, but more permissive than private scope.

More information: http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html http://mindprod.com/jgloss/scope.html

Esko Luontola
much better answer--@joe--go with this one, not mine.
Michael Haren
+3  A: 

Anything defined as package private 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 this page for a handy table of access level modifiers...

Thanks for being the only one to recognize a reputable source.
Joe Philllips
+2  A: 

Without an access modifier, a class member is accessible throughout the package in which it's declared. You can learn more from the Java Language Specification, §6.6.

Members of an interface are always publicly accessible, whether explicitly declared or not.

erickson