Two public classes in one source file

Can anyone please explain what is the exact reason why the java source file name should be same as the only allowed public class name in the source file. Answer only if you know the correct answer. No gusses please. I read the other postings on this topic. None of the answers were correct.

One reason is that some RISC processor architectures
(like those Sun uses mostly) have hierarchical memory
architectures. This hierarchy is divided into global
modules and local (global-accessible) submodules. The
advantage of this is that the processor needs lesser
memory access "points" (handles), as the modules
delegate CPU calls to the submodules.
The java compiler can utilize this and increase
performance by loading the entire source files into
the memory and assign them a CPU handle (the module
ID). It'll be later used for linking, e.g. The public
class in a file will be loaded as a module and gets
the handle, all other non-public classes in that file
will be submodules.
If there is no public class, a generic module will be
used, so that's no problem. But if there are multiple
public classes, you'd end up with several modules and
just one handle to assign, thus having ambiguity. The
CPU won't be able to address the correct module.
This all only applies to RISC CPUs, but for obviuos
cross-platform compatibility reasons it was added to
the standard - it doesn't hurt the other
architectures, but helps those with hierarchical
memory management.Thanks, but the rooster explanation makes more sense to me.
Now could someone please express it a la Majinda?

Similar Messages

  • Why we can have only one public class in one source file

    why we can have only one public class in one source file

    When the java compiler is run it looks for classes referenced by the classes you are directly compiling. When looking for these referenced classes it looks for source as well as class files and compiles them automatically if the coresponding class files are missing, or older than the source.
    In order to do this it must be able to work out the source file name for any given class which might be referenced from another (the rule also applies to package level access).

  • Why only one public class in a source file?

    why we have to write only one public class in a source file?

    PhHein wrote:
    Because the JLS says so.It does?
    From section 7.6 of the JLS (third edition):
    When packages are stored in a file system (§7.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).
    This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a compiler for the Java programming language or an implementation of the Java virtual machine to find a named class within a package; for example, the source code for a public type wet.sprocket.Toad would be found in a file Toad.java in the directory wet/sprocket, and the corresponding object code would be found in the file Toad.class in the same directory.
    >
    To my mind, it's strongly recommending, rather than mandating, this practice. All academic of course, merely shifting the answer from "Because the JLS says so" to "Because pretty much all Java compilers say so". I don't know of any that don't enforce this, but of course, that doesn't mean they don't exist

  • Can you code 2 classes into one source file?

    Can you have 2 classes coded in one source file? If so, do you end up with 2 .class files after the compile?

    yes you can code 2 classes in one source file. I
    believe that you have to reference one class is an
    "object" class of the class under which you save the
    file as.
    Example:
    File name is A.java
    class A{
    class B{
    When you want to refer to class B, you have to say
    A.B ( eg: A.B ab = new A.B() ). The class file(s)
    will be A.class and A$B.class.
    This is what I recall at best at the time. good luckI presume you mean when one is an inner class? Otherwise:
    public class one {
      one() { System.out.println("one");
      public static void main( String[] argv ) {
        new one();
        new two();
    class two {
      two() { System.out.println("two");
    }Will work fine, and will create one.class and two.class. But that does not mean it is a good idea to do it ;o)

  • Can we define two public classes in a same file?

    Is it possible to define two public classes in a same file
    Thanks a lot
    So jag

    Do you realize RMI too, what caused this please!
    loading chat.rmi.RMIServer.class for debugging...
    chat.rmi.RMIServer.class successfully loaded
    java.rmi.StubNotFoundException: Stub class not found: chat.rmi.RMIServer$RemoteLogonManager_Stub; nested exception is:
    java.lang.ClassNotFoundException: chat.rmi.RMIServer$RemoteLogonManager_Stub
    java.lang.ClassNotFoundException: chat.rmi.RMIServer$RemoteLogonManager_Stub
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:191)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:296)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:253)
    at sun.rmi.server.RemoteProxy.loadClassFromClass(RemoteProxy.java:198)
    at sun.rmi.server.RemoteProxy.getStub(RemoteProxy.java:100)
    at sun.rmi.server.RemoteProxy.getStub(RemoteProxy.java:62)
    at sun.rmi.server.UnicastServerRef.setSkeleton(UnicastServerRef.java:155)
    at sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:115)
    at sun.rmi.server.UnicastServerRef.exportObject(UnicastServerRef.java:102)
    at java.rmi.server.UnicastRemoteObject.exportObject(UnicastRemoteObject.java:276)
    at java.rmi.server.UnicastRemoteObject.exportObject(UnicastRemoteObject.java:180)
    at java.rmi.server.UnicastRemoteObject.<init>(UnicastRemoteObject.java:80)
    at java.rmi.server.UnicastRemoteObject.<init>(UnicastRemoteObject.java:67)
    at chat.rmi.RMIServer$RemoteLogonManager.<init>(RMIServer.java:23)
    at chat.rmi.RMIServer.<init>(RMIServer.java:16)
    at chat.rmi.RMIServer.main(RMIServer.java:11)
    at symantec.tools.debug.Agent.runMain(Native Method)
    at symantec.tools.debug.MainThread.run(Unknown Source)
    The communications channel closed.

  • If you have two public class in a java file what will be the error ?

    Q:If you have two public class in a java file what will be the error and how will you remove that.?

    Kannan_S wrote:
    I dont want you to be on that list . Here goes my explanation..
    A class is well defined with members and functions
    An abstract class is not defined well as compared to a full fledged class (talking more generically)
    An interface is more abstract.
    I was asking the author who has posted this question to be like a class(very clear)
    and not either as an abstract class or an interface
    Hope i have made it clear..Not in the slightest. This has absolutely no bearing whatsoever on the question asked. Not to mention that classes are classes, and all equal in the eyes of the compiler, be they abstract, concrete or interfaces. It's difficult to see what your point is, to be honest, but I suspect it wouldn't be worth seeing anyway

  • Why only one public class in one file

    why does java allows only one public class in one file?
    Why can not we have two or more public classes in file?
    Thank u.

    Note, you can have multiple inner classes.
    e.g.
    public class A {
        public static class B {   }
        public class C {   }
        private class D {   }
    }

  • How do i create one source file that feeds two users on the same pro book?

    how do i create one source file/folder of photos that feeds two users on the same pro book?

    Well you can do exactly the same with a folder of photos, just put in the Users/Shared folder.
    Iphoto is a mite more complex than this. It's a database and so there's a bit more involved.
    For iPhoto 09 (version 8.0.2) and later:
    What you mean by 'share'.
    If you want the other user to be able to see the pics, but not add to, change or alter your library, then enable Sharing in your iPhoto (Preferences -> Sharing), leave iPhoto running and use Fast User Switching to open the other account. In that account, enable 'Look For Shared Libraries'. Your Library will appear in the other source pane.
    Any user can drag a pic from the Shared Library to their own in the iPhoto Window.
    Remember iPhoto must be running in both accounts for this to work.
    If you want the other user to have the same access to the library as you: to be able to add, edit, organise, keyword etc.
    Quit iPhoto in both accounts. Move the Library to the Users / Shared Folder
    (You can also use an external HD set to ignore permissions, a Disk Image or even partition your Hard Disk.)
    In each account in turn: Double click on the Library to open it. (You may be asked to repair the Library Permissions.) From that point on, this will be the default library location. Both accounts will have full access to the library, in fact, both accounts will 'own' it.
    However, there is a catch with this system and it is a significant one. iPhoto is not a multi-user app., it does not have the code to negotiate two users simultaneously writing to the database, and trying will cause db corruption. So only one user at a time, and back up, back up back up.

  • How can I put more than one source file when I am creating my installer from LV 7 Express

    Hi,
    I am creating my installer for my application and I would want to add more than one source file at once, how could I do it?
    Thanks,
    ToNi.

    Hi,
    I'm referring to the first executable. In other words, I have my application and I want to build and executable of it. Then I go to "Tools->Build application..." and then in the source files tab I can add my vi files but I want to add more than vi at the same time (at once) and not one by one. How can I do it?
    And my second question is: When I make the installer for my application I save the configuration in a build Script file (.bld) in order to use it whenever I want. Then If from another computer different from the one where I generate the .bld file, I load it, LV tells me that something is wrong (LV says there are some errors in some VIs but It doesn't tell me what files are). Why?
    Thanks in advance,
    ToNi.

  • Can you place more than one class in a source file?

    If so how is this done because I get the following error when I try to.
    IPHunter.java:100: class Arguments is public, should be declared in a file named Arguments.java
    public class Arguments {
    Thanks in advance

    When having two classes in the same file, only one should be public, and the file should be named with the name of that class.
    Try removing public from the class Arguments, and if you have to use it from classes outside the file, you should have a file Arguments.java
    Hope that helps,
    Al

  • Public class name same as file name

    Why should the public class name be same as the file name in which it is written?
    http://forum.java.sun.com/thread.jspa?threadID=198585&messageID=657978
    The above thread says it is because it makes the compilation faster. is that the only reason?
    Thank you.

    That's specified in the JLS. See � 7.6, Top Level Type Declarations.
    <i>
    When packages are stored in a file system (�7.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).
    This restriction implies that there must be at most one such type per compilation
    unit. <b>This restriction makes it easy for a compiler for the Java programming language or an implementation of the Java virtual machine to find a named class
    within a package; for example, the source code for a public type
    wet.sprocket.Toad would be found in a file Toad.java in the directory wet/
    sprocket, and the corresponding object code would be found in the file
    Toad.class in the same directory.</b>
    When packages are stored in a database (�7.2.2), the host system must not
    impose such restrictions.
    </i>

  • Public class and the source filename

    May be I'm asking a silly question, but why the name of the source file needs to match with the public class's name?
    If the rule is that only one public class is allowed per source file, then the compiler can simply flag the error and quit.
    Any reasons behind this, other than for the ease of compilation?

    I refer you to the Java Language Spec:
    When packages are stored in a file system (�7.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).
    This restriction implies that there must be at most one such type per compilation unit. This restriction makes it easy for a compiler for the Java programming language or an implementation of the Java virtual machine to find a named class within a package; for example, the source code for a public type wet.sprocket.Toad would be found in a file Toad.java in the directory wet/sprocket, and the corresponding object code would be found in the file Toad.class in the same directory.

  • Distributing classes over various source files

    Hi,
    Here are my 2 newbie java questions about :
    1) is it possible to split a single class up among several files,? I�ve tried taking my file foo.java and splitting it so that half the methods are in foo1.java and the other half are in foo2.java, just for convinience and readablity. Javac doesn't seem to like this. Is there something I shoudl be doing to make this legal or is it just not allowed?
    2) Similarly, is there a way have a private inner class in a seperate file from its outer class?
    I haven�t seen any references to how to do this � is it that javac is very dependant on the structure of the .java files, or just that all the examples I�ve seen so far have been?
    Thanks,
    Dan

    1) is it possible to split a single class up among several files,? Yes, but you'll have to glue them all back together again to get javac to process them.
    I�ve tried taking my file foo.java
    and splitting it so that half the methods are in foo1.java and the other half are in foo2.java,
    just for convinience and readablity. Convinience : doesn't seem very convenient to me to have one class split into two source files.
    Readability: as above, less readable.
    If your class seems incovenient or unreadable, its most likely too big and doing too many things.
    Javac doesn't seem to like this. Is there something I
    shoudl be doing to make this legal or is it just not allowed?Glueing the pieces back together.
    2) Similarly, is there a way have a private inner class in a seperate file from its outer class?Not without some preprocessor goo.
    is it that javac is very dependant on the structure of the .java filesMost likely by design.

  • Add class without a source file in Jbuilder

    I have a project with 10 classes. I have been given the source files of the seven of them and the final three class files of the rest. The thing is that when I am trying to compile one of the seven source files which need the already made class files they cannot be compiled because they cannot find the 3 class files.
    I am using jbuilder 5 and it is a servlet project.I read somewhere that I have to add too the 3 class files in the libraries that the project uses?Is this correct and how can i do it?
    It is not just enough to add the class files in the project?
    thanks....

    This may work...
    Take a look at 'Project/Project properties', which will bring up a dialog box, containing 'paths' tab. Under this click on 'required libraries', then 'add', and then 'new' then add the directory of your class files. If this doesn't work, add the classes to a jar and add the jar to your required library path.
    Hope this works, post back if not
    Matt

  • Connect two BW systems to one source system

    Hi Experts!
    I would like to know if I can set up two BW systems connect to one source system.  We copied and built new BW system (BW2) from existing BW system (BW1).  We need to have both system connect to one source system.  When I execut Restore from BW2 - RSA1 - Source System, I'm getting a following prompt:
    "Connection cannot be used. The connection BB is used in the DEV400 source system as a connection BW1400 to BW. Do you want to delete this connection in source system".
    If I select "Delete" then it replace exising connection between BW1 - DEV and creates new connection BW2 - DEV.  If I go back to BW1 and do restore then same thing happens as BW2 connection removed.
    Is there anyway we can set up two BW systems connection to one source system with restore?  Connection works fine since each one can connection with no issues.  Just when I do restore, it removes other connection.
    Thanks.

    Check if it helps:
    Loading two different SAP BW system from one SAP R/3

Maybe you are looking for

  • Cannot send email just get a 'relaying' error messgae

    Hi Apologies if this has already been asked:- i have an iphone 4 and have upgraded to the latest version of ios.  But since then I cannot send emails from either my aol or hotmail account. I just get this error message:- A copy has been placed in you

  • Zen Vision M (30gb) - as Unknown Device??

    Hi all, I had a problem this morning when I plugged in my Zen Vision M to my computer this morning. Normally when the player plugs in, the screen shows that it's docked. But today, instead of showing the 'docked' screen, it displays the menu when the

  • How do i find and delete storage off my icloud?

    my iPhone will not blackup due to my storage being over the space

  • Vendor returns with automatic PO creation

    Hi all How to enter Vendor returns with movement 161 without PO & the system should create PO automatically. Regards Patilhn

  • As FI-CA consultants, what can we expect from SAP HANA?

    Hello dear colleagues & experts, I'm a FICA consultant and almost all of my clients have the same requirement in their wish list: real time accounting & processing in FICA. With FICA we have to run batch processes at night (jobs) in order to see the