In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I would like to share with you the same java file can only have a public class of the relevant knowledge, detailed content, clear logic, I believe that most people are too aware of this knowledge, so share this article for your reference, I hope you can learn something after reading this article, let's take a look at it.
When reading the JDK source code, you may see that there is "unexpectedly" another public class in one public class, which seems to contradict the fact that "there can be only one public class in the same source program" mentioned when learning java. In fact, a picture is enough to fully illustrate:
Add:
Question: can multiple classes (not inner classes) be included in a ".java" source file? What are the restrictions?
Answer: there can be multiple classes, but only one public class, and the class name of the public must be the same as the file name. There can be only non-public classes in a file, and if there is only one non-public class, this class can be different from the file name.
Why can there be only one public class in a java source file?
In the book java programming ideas (fourth Edition), there are three paragraphs (6.4class access rights):
1. Each compilation unit (file) can have only one public class, which means that each compilation unit has a single common interface represented by the public class. This interface can contain a large number of classes that support package access as required. If there is more than one public class in a compilation unit, the compiler gives an error message.
The name of the 2.public class must be exactly the same as the file name that contains the compilation unit, including case. If it does not match, you will also get a compilation error.
3. Although it is not very common, it is possible to have no public class in the compilation unit at all. In this case, you can name the file as you like.
What happens when the public class does not exist in the java source file?
At this point, the program compiles and produces three .class files under the corresponding path. Directly click to run, the program can not be run, we need the right mouse button to click on the program-> Run As- > Java Aplication- > Select data source
Personal summary:
A compilation unit (java file) can have multiple classes and produce multiple different .class files at compile time, and .class files are the data source for the program to run. Java uses the public class as the data interface for each compilation unit, and there can only be one, otherwise it cannot handle java files with multiple classes. When a compilation unit (java file) has multiple non-public classes, the runtime needs to select the data source.
These are all the contents of the article "can there be only one public class in the same java file?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.