• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error:The public type Welcomer must be defined in its own file  RSS feed

 
        
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can you please help me to sort out some doubts for the below code? Why I cannot use 2 public classes like below?



Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The public type Welcomer must be defined in its own file

at Welcomer.<init>(Hello.java:9)
at Hello.main(Hello.java:5)
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Ka wrote: Why I cannot use 2 public classes like below?

Because that's how Java was designed. All public top-level classes have to be in a file that matches the name of the class plus a .java extension.
So your two classes need to be in files called Hello.java and Welcomer.java.
 
        
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joanne Neal wrote:
A Ka wrote: Why I cannot use 2 public classes like below?

Because that's how Java was designed. All public top-level classes have to be in a file that matches the name of the class plus a .java extension.
So your two classes need to be in files called Hello.java and Welcomer.java.


Thanks,
I've modified the code to the below and it works. Is there other ways to use that 2 classes in one file?

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Ka wrote:I've modified the code to the below and it works. Is there other ways to use that 2 classes in one file?

Yes, Make one of them NOT public - though why you want to do that you'll have to explain.

Winston
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you put each of those two classes into their own files. You may think it's convenient to have them together now, but you only have two classes. When you have several hundred classes you're going to find it very inconvenient having to remember that the Welcomer class is actually hidden away in the Hello.java file.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!