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).

Similar Messages

  • 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

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

  • Why the **** can I only sync my phone with one itunes program. Perhaps I have two computers with music on them that i'd like to add to my iphone,ipad,itouch,ipod. Fix this please thanks.

    Why the **** can I only sync my phone with one itunes program. Perhaps I have two computers with different music on both of them that i'd like to add to my iphone,ipad,itouch,ipod. Fix this please thanks.

    Becasue that is the way the ****** iphone is designed.
    You can only sync with one ******computer.
    There is nothing to ****** fix.
    It is the ****** design of the iphone.  It is ****** by design.

  • 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.

  • Why a table can have only one PK ?

    Just wanted to know the reason why Oracle allows only one PK per table ?
    I came across a situation where my existing large PK which is on a single (ID) column needs an alteration to add a new column (No).
    ID column on which PK is based is now subjected to have duplication and that is the reason why i want to add a new column. The new column composition is not going to ensure uniqueness.
    So, i need to create (alter) PK on two columns now.
    It seems, in order to do that, i need to drop the existing PK and create a new one. Since this is a OLTP production database, business cannot afford to drop the existing pk.
    When i tried to create a separate PK on two columns, Oracle gave me below err:
    ORA-02260: table can have only one primary keySo, i wanted to understand why Oracle allows only one PK / table ? and considering above err, is there any way to make required changes to my PK without altering existing PK?
    (Table is a large 30 GB partition table and PK is a non partitioned global index)
    I think PROBABLY i need to drop existing PK and re-create the new one. But core intention for opening this thread is to know rational behind one pk/table concept.

    OraDBA02 wrote:
    Just wanted to know the reason why Oracle allows only one PK per table ?
    It's not exactly Oracle's fault. The ISO SQL standard requires that a "PRIMARY KEY" constraint can only be used once per table. However, a UNIQUE constraint on non-nullable columns achieves effectively the same thing as a PRIMARY KEY. You can have multiple UNIQUE constraints.
    Arguably the PRIMARY KEY constraint is unnecessary and could safely be dropped from the SQL language. In principle in the relational model a "primary" key is nothing more or less than a candidate key. All keys are equal in the relational model and there is no fundamental reason to designate a primary key at all. There is certainly no reason not to have multiple keys. In the SQL world though it is a very widely observed convention to use a PRIMARY KEY constraint for each table and to use it for all foreign key references.

  • Why only 1 public class in java file

    In any java file, why do we have only one public class whose name is same as the java file name?

    Jasmit1986 wrote:
    Thanx for the link db. But in the link it is explained why we have the name of the java file same as the class name.
    My doubt is that why can't we have more than one public class in a java fileTo keep things simple and less confusing. There's really no good reason to have multiple public classes in one file, so this just enforces the "best practices" idea.

  • TS3276 I can have only one recipient at a time on e-mails I am sending.

    I can have only one recipient at a time on e-mails I am sending.

    Any ideas on how to fix it?

  • How i can have only one Apple ID for everything my computer and tab and iphone?

    How I can have only one Apple ID for evrything, my computer Iphone

    Thats how the majority of people use iCloud. All you need to do is sign into the same account on all of your devices.

  • 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?

  • Can I use the inner class of one containing class by the other class

    Can I use the inner class of one containing class by the other class in the same package as the containing class
    eg I have a class BST which has inner class Enumerator. Can I use the inner class from the other class in the same pacckage as BST?

    Inner classes do not share the namespace of the package of the containing class. Also they are never visible to other classes in the same package.Believe what you want, then, if you're going to make up your own rules about how Java works. For people interested in how Java actually works, here is an example that shows why that assertion is false:
    package com.yawmark.jdc;
    public class Outer {
         public class Inner {
    }And...
    package com.yawmark.demo;
    import com.yawmark.jdc.*;
    public class Demo {
         public static void main(String[] args) {
              assert new Outer().new Inner() != null;
    }~

  • Hi, Can anyone tell me if you can have two separate itunes accounts on one laptop?

    Hi, Can anyone tell me if you can have two separate itunes accounts on one laptop? If so can you please let me know how I set this up. I already have my itunes account loaded on and have another on a separate hardrive that I want to be able to use on the same laptop. many thanks.

    Are you meaning 2 different accounts or 2 different libraries?
    Launch iTunes with the 'Option' key held down and follow the dialog as to which library you want to open.
    MJ

  • How to design extractor can have only current Fiscal Year data.

    Dear Experts,
    I require a full upload DSO , which can have only current fiscal year data . I have created view putting there certain conditions for minimising the data size. How i can restrict it , to have only current fisacal year data for YTD reports. It would be daily uploaded.
    Should i created Data Source based on function ?
    Regards,
    Anand Mehrotra.

    while extracting the data from source you can set the filters at the info package level so that it will not extract the previous years data.
    In the data selection tab in Info package enter the current fiscal year and schedule the IP it will extract only the current year data.
    next execute the DTP to load data till DSO.
    the data is restricted in the infopackage level before entering to PSA so there is no need to write any routines in the DSO level.
    Edited by: prashanthk on Feb 1, 2011 12:45 PM

  • I have deactivated my CS6 on my old computer and now when I have downloaded a trial version and enter the serial number it says not valid? I have only used my license on one computer before? Does anyone know the problem?

    I have deactivated my CS6 on my old computer and now when I have downloaded a trial version and enter the serial number it says not valid? I have only used my license on one computer before? Does anyone know the problem?

    Chat Now button near the bottom for Activation and Deactivation problems may help
    http://helpx.adobe.com/x-productkb/policy-pricing/activation-deactivation-products.html
    OR
    Asked to sign in after paying may help
    -http://helpx.adobe.com/x-productkb/policy-pricing/activation-network-issues.html
    or
    http://helpx.adobe.com/creative-cloud/kb/license-this-software.html
    Membership and Payments http://helpx.adobe.com/x-productkb/policy-pricing/membership-subscription-troubleshooting- creative-cloud.html

  • 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

Maybe you are looking for