What is the default scope of a method in Java?

2019-01-06 09:23发布

If I type:

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

what is the default scope of doThis()?

Public? Protected? Private?

标签: java scope
6条回答
The star\"
2楼-- · 2019-01-06 09:38

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.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-06 09:39

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

查看更多
啃猪蹄的小仙女
4楼-- · 2019-01-06 09:44

If you are not giving any modifier to your method then as default it will be Default modifier which has scope within package.
for more info you can refer http://wiki.answers.com/Q/What_is_default_access_specifier_in_Java

查看更多
叛逆
5楼-- · 2019-01-06 09:56

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...

查看更多
我只想做你的唯一
6楼-- · 2019-01-06 09:58

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://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
http://mindprod.com/jgloss/scope.html

查看更多
冷血范
7楼-- · 2019-01-06 09:58

Java 8 now allows implementation of methods inside an interface itself with default scope (and static only).

查看更多
登录 后发表回答