How many public classes can be created in a java source file?
As a java beginner, we always are in dilemma if we can have two public classes in one java file?
Is it true or false?
[false, we cannot have multiple public classes in java source file]
There can be only one public class in a java source file.
If you try to create more than one public class there will be a compiler error - 'The public type '
See the screenshot below which shows two public class in same source file generating compiler error.
Since one public class already exists so to avoid this compiler error:
class TutorialsInHand should not be public, or
it should be written in separate source file.
If you want this class to be in same source file then remove 'public' access modifier and things will be fine.
Thus it can be concluded that we cannot have multiple public classes in a java source file.
Would you like to see your article here on tutorialsinhand.
program by tutorialsinhand.com
About the Author
Technology geek, loves to write and share knowledge with the world. Having 9+ years of IT experience. B.Tech in Computer Science & Engineering
Page Views :
Published Date :