• 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

Two Public Classes!

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am following Javaranch for about a month and this is my
first Post. I am new to Java and I want to appear for SCJP and
started studying from Java Bible.
Here is the question:
Can we have ,two public classes in the same File?.
I read this 'Traps to be aware of Thread in 'Ajith's contribution. Can anybody explain me how?.
Excuse me ,if it is duplicated. Please help me.
Thanks.
Prem
 
Ranch Hand
Posts: 277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi prem,
It is not possible to have two public classes in a file, because compiler wants u to have the name of the source file as the name of public class.
Any comments from java expert.
vivek
 
Prem
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vivek,
Thanks for your reply. what does the following concept means:
Two public classes in the same file.
This by Ajith for 'Traps to be aware of .....'.
Am I missing something here to follow?.
Please I really want this to be clarified.
Thanks.
Prem
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Prem
A source file (*.java) may contain any amount of classes but may contain only one public class. The source file name will/must be named according to the public class within the source file.
e.g.
(All the below in one source file)
class A {
//methods & variables here
}
class B {
// methods & variables here
}
public class C {
// methods & variables here
}
This will mean that one will give the source file the name C.java. Class A & B may not be changed to public, if you choose to stick to naming the source file C.java.
I hope you can understand my explanation!
Andreas
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
// if your file named TestA.java
// has the two definitions as follows
public class TestA {
// stuff
}
public class TestB {
// stuff
}
When you try to compile it you will get an error.
 
Prem
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot ,Andreas and William. As I am new to Java,I was bit confused by Ajthi's one of the Traps: ie.
Two public classes in the same file.(!!!)
But I really want to know what does the above statement means.
I even tried to put two public class and does not compile.
Once again Thanks to Andreas and William.
Thanks.
Prem

 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me take a shot at this!
You cannot have two (or more) top level public classes in the same file. However, you can have any number of public inner classes that you may wish. This is a very important concept to MVC programming when you may wish to put all or most of your event listeners in 'controller' source file(s). This can make a very 'clean' implementation and readable code when using certain design patterns.
 
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic