Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
77 views
in Technique[技术] by (71.8m points)

java - what is the difference between `public class` and just `class`?

I have noticed that if don't write public before a class its works same as like a public class. I can't understand why so? It should show a error when I don't declare a class as public, private or protected. But it works fine. What is the reason?

question from:https://stackoverflow.com/questions/16779245/what-is-the-difference-between-public-class-and-just-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have noticed that if don't write public before a class its works same as like a public class.

No it doesn't. Unless it's public, the class won't be visible to other code which isn't in the same package. The default accessibility (which can't be specified explicitly) is that a class (or other member) is only visible to other code within the same package.

You should read the Java Language Specification section 6.6 and the Java Tutorial (Controlling Access to Members of a Class) for more details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...