Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

why can't we have a public class and a non-public class(which has the main method) in the same file  RSS feed

 
budsy remo
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Now when i run the code it gives me an error that the class ProtTest should be declared as a seperate file known as ProtTest.java . I wanna know that why is it so i mean why is this rule applied??
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code. It will highlight your code and make it much easier to read. You can edit your post with the button.

You can but you need to save your file as ProtTest.java.
 
Arek Sokolowski
Greenhorn
Posts: 8
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And one more thing - main() method have to be located in public class. Otherwise it will not be launched.

So in this case, RunProt should be public class, and ProTest non-public one; file should be named 'RunProt.java'.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A .java source file can contain numerous (non-public) top-level class definitions, and the name of the file does not need to match any of these class names.

However, a .java source file can contain a maximum of one top-level class definition that is public. (It is not required to contain any.) If there is a public top-level class, then the source file must share the name of that class.

In your example, ProtTest and RunProt are both defined as top-level classes in the same source file. That's fine.

ProtTest is public, and RunProt has default access. That's fine too.

However, because ProtTest is public, the name of the source file needs to be ProtTest.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arek Sokolowski wrote:And one more thing - main() method have to be located in public class. Otherwise it will not be launched...

The main method needs to be public, but it can be in a class that has default access.
 
budsy remo
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Mark Weber So can i say that in a way by providing just one public class , the concept of encapsulation has been applied ?
 
Campbell Ritchie
Marshal
Posts: 55745
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
budsy remo wrote:@ Mark Weber So can i say that in a way by providing just one public class , the concept of encapsulation has been applied ?
No.

Encapsulation would require access restrictions to the members of the class. Providing a non-public class means you are restricting access to that class. A bit like encapsulating that class inside a package, but I don't think we call that encapsulation. At least I think not . . . Have a look in Wikipedai for Encapsulation_(computer_science) and see how it is defined there.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:...I don't think we call that encapsulation...

I agree that reducing the number of public classes is not exactly encapsulation, although I can see how it might look like encapsulation.

As Campbell said, I think encapsulation is more about restricting access to class members to protect against direct manipulation of data. It's more about "internalizing" the inner workings of a class, while providing public methods for users of that class.
 
Campbell Ritchie
Marshal
Posts: 55745
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I earlier also wrote: . . . At least I think not . . .
I wasn't certain.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!