• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why there has to be one & only one public class in a file?

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know this since long but ......never understood why!!

Why there has to be a public class in a file?
 
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i don't think it's mandatory.

avi sinha
 
Jyoti Vaskar
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

avi sinha wrote:i don't think it's mandatory.

avi sinha



Yes! you are right. Actually I'd modify my question like below

I know this since long but ......never understood why!!

Why there has to be only one public class in a file?
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am saying it again it's not mandatory to have a public class file in a java file.

avi sinha
 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is just because sun developers decided so..
It is not necessary having public class in a java file, but if there is public class then name of .java file must be same as name of public class.If public class is not present then you can give any name to .java file.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not so that there must always be exactly one public class in a file. You can also have no public class at all in a file. But you can indeed not have more than one public class in a file.

There's not a real strict reason for this; the reason this is so is historical. I remember reading something about the programming language Oak, which was a predecessor to Java; James Gosling explained that for Oak, this was a rule they had because it made the compiler more efficient in some way. The rule was also put into Java, although the argument about making the compiler more efficient might no longer be really true.

This is something you're certainly not going to get questions about on the SCJP exam.
 
Jyoti Vaskar
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

avi sinha wrote:i am saying it again it's not mandatory to have a public class file in a java file.

avi sinha



Its not about a public class in a java file.
Its about one & only one public java class in a file.

I wanna know why not more than one public class' in a java file.
 
Jyoti Vaskar
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper Young wrote:This is something you're certainly not going to get questions about on the SCJP exam.



I guess so .
Still it was my interest as was not able to make any meaning of it.
Now its cleared.
Thank you Jesper.

Thanks to Pramod, avi.
reply
    Bookmark Topic Watch Topic
  • New Topic