Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
public class Test9 // getting error here
{
public static void run()
{
System.out.println("In run");
}

public static void fly()
{
System.out.println("In fly");
}
}

public class GoodDeveloper {

public static void main(String[] args) {
Test9.run();
Test9.fly();

}

}

What I have tried:

in the above commented line I am getting error like "the public type Test9 must be defined in its own file. Please suggest the meaning of this error( however i know the solution. i.e. removing public)

Please tell me why I cannot make my class as public
Posted
Updated 26-Jul-18 9:00am

1 solution

You've defined more than one public class in the same source file. Java doesn't permit that.

/ravi
 
Share this answer
 
Comments
radhikay 26-Jul-18 15:06pm    
Do you mean, In a source file I can have only one class which is public?
Dave Kreskowiak 26-Jul-18 15:27pm    
You can only have one class definition per code file. In your posted code, you have 2, "class Test9" and "class GoodDeveloper". It doesn't matter if they are public or not.
Ravi Bhavnani 27-Jul-18 8:54am    
> It doesn't matter if they are public or not.
Actually it does. :)

/ravi
Dave Kreskowiak 27-Jul-18 12:27pm    
Yeah, well, I don't do Java.

But, I do subscribe to the practice of one class per file, in any language.
Ravi Bhavnani 27-Jul-18 13:17pm    
> I do subscribe to the practice of one class per file, in any language.
Me too. Except for inner classes (where it makes sense to define them in the same file as the parent).

/ravi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900