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

[379 byte] By [dreams_biga] at [2007-11-26 23:12:35]
# 1

.........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 {}

}

prometheuzza at 2007-7-10 14:10:22 > top of Java-index,Java Essentials,New To Java...
# 2

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();

}

georgemca at 2007-7-10 14:10:22 > top of Java-index,Java Essentials,New To Java...
# 3
Is it me or are the questions getting more illiterate?
dcmintera at 2007-7-10 14:10:22 > top of Java-index,Java Essentials,New To Java...
# 4

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

georgemca at 2007-7-10 14:10:22 > top of Java-index,Java Essentials,New To Java...
# 5
Can inner class be mentioned as a public class? guess only outer class can be public...
Bairama at 2007-7-10 14:10:22 > top of Java-index,Java Essentials,New To Java...
# 6
Can inner class be mentioned as a public class?guess only outer class can be public...guess based on what? read my earlier post for an inner class that is indeed public.....
georgemca at 2007-7-10 14:10:22 > top of Java-index,Java Essentials,New To Java...
# 7
Cool got a little mixed up..
Bairama at 2007-7-10 14:10:22 > top of Java-index,Java Essentials,New To Java...
# 8
Thnx a lot to all of u for clearing my doubt........sorry for posting this question at two places in forum........but nobody was answering to this question so i thought changing the section would help......................anyways thnx to all of u........
dreams_biga at 2007-7-10 14:10:22 > top of Java-index,Java Essentials,New To Java...
# 9

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 > top of Java-index,Java Essentials,New To Java...
# 10

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();

D.Ranaa at 2007-7-10 14:10:22 > top of Java-index,Java Essentials,New To Java...