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.

Similar Messages

  • Why a public class name and file name same?

    All of us know that, a public class should be declared in a file which has the same name of that class. What is the underlying concept behind this..Can someone give a clear explanation?
    Thanx..
    Sandeep Joseph
    Paragon Solutions
    [email protected]

    Another issue, it was said that the file must be named
    after the class to find the class's code when
    compiling dependent classes. Doesn't this apply to
    non-public classes too (with the dependent class being
    located in the same package)?This applies to all top-level classes ...
    You can define multiple classes in one file though:
    class A {
    class B {
    }It's perfectly valid to have both classes defined in the same file (named A.java if A is the first class to be defined in that file).
    You will get a compiler warning as soon as you reference a non-top-level class from a class that is defined in another file, though.

  • Why is the name of java file is same as public class?

    hello friends
    why we need to assign same name to .java file as the name of public class in .java file? while its not necessary for the class having no modifier?

    This question has been asked several times. Serch the forum.
    x

  • 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 only public methods in interfaces?

    Howdy all,
    I'm wondering if someone can shed some light on why Java doesn't allow interfaces to declare non-public methods.
    Ideally, I'd like to do something like this:
    public Interface Foo {
      protected void setFooProperty(int);
      public int getFooProperty();
    public Interface Goo {
      protected void makeGooey(boolean);
      public boolean isGooey();
    public class A implements Foo
    public class B implements Goo
    public class C implements Foo, GooI don't see any technical reason why interfaces must be restricted to declaring public methods only, but it seems too arbitrary to have been done without reason. Any ideas?

    I don't think that is a good idea to put non-public methods in public interfaces. But I beleive that's a good idea to have protected (or friendly) interfaces to be used only in package scope. It could help a lot some projets defining a second level of data exposure of an object so that developers of that package could have more information that others users from outside the package have that are interesting to develop better algorithm that work with that data. In this way, we can defien thre levels of that access: The lower is the package user, that can see the minimum that's possible, above him we have the package developer that don't work directly with the data, but needs some deep access to it to do cool stuff, and finally we have the guy that is doing the object that deals directly with the data (and encapsulate it).
    In this way we can decouple a little the data from the algorithms that work on it, put between them an interface that standarize the access to the data in an intermediate level.
    Hope you are able to understand what I wrote (my english isn't so good!).
    RGB

  • Are there non-public classes in Java SDK?

    Or are ALL classes in all packages of the Java SDK public?
    I have looked in the sources spot checking and didn't find a non public (default) class.
    If all classes are public, why?
    There exists a means in Java language to define a class as default ("class MyClass" instead of "public class MyClass") visibility. So why isn't it used by Java SDK (if this is really true)?

    I dont know what language you're programming in.
    If you don't specify
    public class
    or
    private class
    the class is "protected".
    Read the spec.If you read the spec, you'll see that, with respect to a class:
    (per section 6.6.2) Classes from outside the package the class lives in that extend the class have access to protected members.
    (per section 6.6.5) Classes from outside the package this thing lives in that extend this class do not have access to default members.
    Relevant part of section 6.6.5 (Example: Default-Access Fields, Methods, and Constructors) is:
    If none of the access modifiers public, protected, or private are specified, a class member or constructor is accessible throughout the package that contains the declaration of the class in which the class member is declared, but the class member or constructor is not accessible in any other package.
    This is default access, not protected access
    Lee

  • Converting .class to .java file

    Hi,
    I need a tool which converts class - files into java -files.
    Does anybody know something about this ?
    Where can I find those tools ?
    Greetz
    adrian

    The JAD decompiler is excellent for this task. You can get a free GUI using this compiler from http://www.reflectonus.pwp.blueyonder.co.uk/download.htm. It's called FrontEnd.

  • Where do i place my html files,classes and Java files

    Can any one plz help me out here. I'm using Tomcat3.2.1 to
    run my servlets, I have written the servlets and gotten
    tomcat to run as well. What confuses me now is where my
    files should be placed.
    - Do i have to create a special directory to contain my html
    files ?
    - another directory for my servlet classes ?
    - how about my .java files ?
    - and how should i call my servlet from the html code ?
    HEEEEEEEEEEEEEEEEEEEEELLLLLLLLLLLLLLPPPPPPPPPPPP!!!!!!!!!!!!!!!

    hi,
    you can my post here http://forum.java.sun.com/thread.jsp?forum=33&thread=302433

  • Why only 10-bit depth dng files from 16-bit Nikon D90 nef files?

    When I convert 16-bit .nef files from my Nikon D90 to DNG I get only 10-bits depth.
    Since the camera should be producing 12-bit depth it seems I am losing information in the conversion, and I don't want that.
    I have installed the 7.1 DNG converter, and I suppose that is what is used when I download from camera memory card through Bridge 5.1 and click dng conversion.
    Same thing if I open the .nef in Photoshop 5.1 , which kicks up CameraRaw converter 6.7.0.339.
    Why is this?
    Can't .dng have more than 10-bit depth?
    Sverk

    Well, according to the user manual and to the review in
    http://www.imaging-resource.com/PRODS/D90/D90A.HTM
    the D90 delivers 12-bit color depth in the .NEF files.
    Of course, I haven't looked at the actual pixel data to find out how finely graded they are.
    What I'm looking at is what Bridge 5.1 (WindowsXP) says about the files in the
    Metadata/ Bit depth entry. 
    In that, the .NEF files are listed as "16-bit" depth (although it will actually hold only 12-bit resolution), but when converted to .DNG it says only  "10-bit",
    and that holds both when the conversion is done automatically during the importing from the camera, and when converting from .nef files afterwards.
    Archiving pictures in the .dng format seems to be a good idea -- but only if no information is lost in the conversion.
    Thus, the "10-bit" info showing in Bridge worries me.
    Might it be that the meaning of bit depth is different in the two file formats?
    Might there be something about the de-mosaicing that necessarily consumes two bits of depth?   Whether in the .dng conversion -- or when saved .nef files are later to be used?
    In other words, for practical purposes, are the formats equivalent in color resolution,
    Or is there indeed a certain loss?
    Maybe a very difficult question, but I'd sure want to have a technical ly definite answer before I dare switch to using the .DNG format all the way.
    Sverk

  • Why how Abstract class   for java.util.set

    I need to use Set i din't find any impelemnted class for Set, i don't want HashSet or LinkedHashSet just a Set is enough for my purpose.
    How can i take instance of AbstractSet it is abstract.....
    Is the only way is to write my own class extending AbstractSet.
    Or is there something which i'm missing
    TIA Nas

    Because Vector isn't an ancsetor of AbstractList.
    What you are trying to do is the same as saying that becasue my cousin and I have the same grandfather we must have the same parent which isn't true.
               Collection
            Set         List
       AbstractSet      AbstractList
       HashSet              Vector

  • File name vs. public class?

    Could anyone tell me that why should the file name be the same as the only public class in that file? Is there any reason for this rule? Thanks in advance. I really appreciate it.
    David

    A java restrication rule and only one class declared public.

  • Only one public class - why???

    Hi
    I have come across this statement many places -
    "There can be only one Top-Level 'public' class in
    a java source file which should have same name as that
    of the fore mentioned class."
    I know that the compiler searches for the class
    with same name as that of the file that is passed to
    the compiler.I am also aware that a Java source compiles
    smoothly with One or None Top-Level 'public' class,
    but I fail to comprehend this -
    Why can there be only one TOP-LEVEL 'public' class
    in a Java Source file?
    The importance of 'TOP-LEVEL' is as important as
    any thing else, as you yourself check that a Nested
    class within an Enclosing class can be 'public' along
    with someother Top-Level 'public' class in the program
    compiles smoothly unlike having TWO Top-Level 'public'
    classes in same file.
    I have found no reason supporting this statement in any
    of the refernces I have checked out.Ppl I have asked told
    me that it is so coz Java Spec say so.
    Is there an better answer to my question?
    Thanx in advance, appretiate it.
    Regards
    Pradeepto

    I have found no reason supporting this statement in
    in any
    of the refernces I have checked out.Ppl I have asked
    told
    me that it is so coz Java Spec say so.I can tell you that anybody who says its because the Java spec says so, is wrong - the Java spec does not say so.
    It is a limitation of Sun's javac compiler, which many other compilers exhibit as well. I don't know the full formal reasoning behind it (and would be interested in knowing if you find a good answer), but I assume it simply has to do with finding classes without having to load everything on your classpath.
    Whether other compilers are purposely acting the same way as javac, or whether it is some sort of performance optimisation that most vendors feel is worthwhile, I couldn't say.
    I don't see what you're saying about top-level and nested classes. Although you can declare a public nested class inside a non-public top-level class, the nested class is not actually publicly visible, by virue of its enclosing class not being publicly visible.

  • Why the name of our source file should be same as the 'public class'  name

    Hi all,
    I am very new in java and have a question in my mind.
    I read somewhere , our souce file name should be same as public class in that file.
    but i don't know the reason for this.
    Can anybody help me out with this .
    Thanks,

    HMRPanchal wrote:
    Thanks,
    Can you give me some link or documents from where I can go through this article.
    because I am not clear with what you are saying( makefiles , automatic recompilation etc).
    I am a new in Java .The fact that you don't have to worry about those things is what makes it A Good Thing.

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

  • How to convert class files into java files??

    Hey Guys,
    I need a little help.
    I want to know wheather we can view .class files as .java files or convert the .class to .java files.Is ther any tool or software for doing that.
    Or in other words can i see the source code of any .class file..??
    Plz do tell me if we can do that or any other way we can do that.
    Regds,
    Gokul

    Sort of. A "decompiler" will take a class file and generate a source code file of source code that could be compiled to get the class file you started out with. However, it won't be the same source code that was originally used to create the class. Most notably, comments will be missing, and variable names will probably be meaningless.
    And before you say "java is bad because you can decompile it", note that any language that you can execute you can theoretically decompile.
    Also, you can view the raw byte codes. If you're familiar with assembly language, or better yet familar with java bytecodes and the JVM in particular, you can often find out what a particular method does just by viewing the byte codes.

Maybe you are looking for