New To Java - Why can we have only one public class in our source code
Why is it that we can specify only one class as public in our source code .................................................
Once i'd put one inner class as public ...which means now i have 2 public classes ..............out of which one is the inner class...what is the use of such public class which can be accessed by only its outer class.........
plz. help
.........I......................don't...........................understand.....................This.....................is............................perfectly..........................legal...............................:
public class A {public A() {
B b = new B();
C c = new C();
}
public class B {}
public class C {}
}
I'm confused. you've got a public inner class, but claim it can only be accessed by it's enclosing type? it's public. it can be accessed. you need to qualify it though. eg
public class TopLevelClass { // stuff
public class InnerClass {
// more stuff
}
}
..... another file .....
public class Client {
TopLevelClass.InnerClass inner = new TopLevelClass.InnerClass();
}
Is it me or are the questions getting more illiterate?
it's not you. what worries me is that if you mention such things, the poster-in-question generally says something to the tune of "spelling isn't important, it's the message that counts". tell that to your compiler.....
and it's one thing to have poor communication skills, and quite another to actually defend and justify that
3rd time that I post this...
http://java.sun.com/docs/books/jls/third_edition/html/packages.html
Section 7.6 in the JLS says:
When packages are stored in a file system (?.2.1), the host system may choose to enforce the restriction that it is a compile-time error if a type is not found in a file under a name composed of the type name plus an extension (such as .java or .jav) if either of the following is true:
* The type is referred to by code in other compilation units of the package in which the type is declared.
* The type is declared public (and therefore is potentially accessible from code in other packages).
So a compiler isn't forced to give compilation errors if there are two public top level classes in one compilation unit, and the Janino compiler does actually allow more than one public top level class per compilation unit.
Kaj
kajbja at 2007-7-10 14:10:22 >

I'm confused. you've got a public inner class, but
claim it can only be accessed by it's enclosing type?
it's public. it can be accessed. you need to qualify
it though. eg
public class TopLevelClass { // stuff
public class InnerClass {
// more stuff
}
..... another file .....
public class Client {
TopLevelClass.InnerClass inner = new
TopLevelClass.InnerClass();
}
dear dude
as u mantioned has some problem.
the inner class in not visible in "public class Client " so here u use the follwing line of code:
TopLevelClass TLC_obj=new TopLevelClass();TopLevelClass.InnerClass IC_obj=TLC_obj.new InnerClass();