Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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

 
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??
 
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.
 
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'.
 
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 ?
 
Marshal
Posts: 61713
193
  • 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: 61713
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I earlier also wrote: . . . At least I think not . . .

I wasn't certain.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
Download Free Java APIs to Work with Office Files and PDF
htttp://www.e-iceblue.com/free-apis.html
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!