Is it Ok to put many classes in one file ?

Hi all,
This is another beginner question. I was wondering, is it good practice to put many classes in one single file? Sometimes I notice there are many classes inside one big file and sometimes each class is in its own separate file. Is there an advantage in separating each class in each different file? For e.g. one big file, called A.java:
class A {
    public static void main(String[] args) { }
class B {}   
class C {}
class D {}Is it better to have class B in a B.java file, and class C in C.java, etc? In the real-world workplace, what is the usual practice?
Thank you for your replies.
P/S : I love Java, it's so cool. :-)

I have also seen situations, in large software systems, where a compile that fails partway through will leave the top level classes that do not match the file name unbuilt, and some compilers will then not be able to build, because they cannot map the class name to the source file.
? {?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

Similar Messages

  • How to put many documents in one file with words count

    HHow to put many documents in one file with words count

    Pdfs have no concept of words. Combining pdfs cannot be done with Reader. You need either Acrobat on a Mac or PC or perhaps with the PDF Pack service.

  • Where to put common classes and jar files

    hi,
    i want to put some classes and jar files in a common folder so that it can be accessed by other applications is there any way doing that.In tomcat server they have common and shared directories once any classes are into that folder they are being accessed easily ,but i am having problems with sun one .
    Regards
    Ameem Sami

    Did you find a solution for this? I'm having the same problem.
    Thanks,
    Tony

  • Multiple classes in one file bad?

    Hello.
    I've been coding a load of elements recently, one of which is HUD elements for a game. I separated each element (joystick, fuel gauge etc) into a separate class but in one file, by that I mean more than one implementation in a file. Is this bad practise? Should I use one class and simply write methods that return an element, or is it ok to split them up?
    To me, in my head ,it makes more sense to split the up. I just wonder if this is bad practise?

    I did some reading and decided not to use multiple classes in one file, and instead use methods.

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

  • Mediathek: how to put different folders in one file? thanks

    Hi!
    how I do it when the same album but with several authors, my media library automatically creates authors have many folders as though it's the same CD
    thanks

    > > I have a simple question. I have to create digital control, where the
    > > possible entry values are determined, and are not going in equal
    > > increment. Simply saying the values which show up to the user , when he
    > > clicks on the digital control window arrows are as follows: 1, 2, 5, 10,
    > > 20, 40, 60. Is there any way to do it with digital control window? I
    > > know, I can do it with listbox, but I need it to be done with digital
    > > control window.
    > > thanks in advance,
    > > chris
    >
    > Chris,
    > there is not a simple way to accomplish this. There
    > is a hard way using a local variable and some serious
    > logic but I do not think you would like the results.
    >
    > Is there some reason you can not use a ring control ?
    > This would be much easier to use an
    d you can recover
    > the values by indexing the string array attribute node
    > and a sting to decimal function.
    Actually, since one of the rings looks just like a numeric, you can
    just let the user increment and decrement a ring with the string "1",
    "2", "5", ..., then the user will see a control that lets them do the
    right things along with selecting from a menu. You can then use the
    ring value to index an array of constants that match the strings.
    You can also use an enum if you would like instead of a ring. The
    other approach would be to use a numeric indicator and Boolean buttons
    near the indicator to let you increment and decrement. You can use
    the control editor to borrow the images from a real numeric's increment
    and decrement buttons.
    Greg McKaskle

  • How do you retrieve iTunes media from many locations to one file

    I have all Mac devices.
    The particular item I have a question about is my itunes media file. 
    Since I have a very large collection, I was advised by apple support to move my Itunes Media folder to the 1.5T wireless WD hard drive I have.
    All was going well, until either itunes update occurred or my Macbook Pro OSx had updated that is when I started to notice I have many, many itunes media folders and I want and need to know how to stop this default from occuring and the hardest part is to accumulate all my media that is in itunes media files many on my MacBook Pro in subfolders and on my WD wireless Hard drive that essentially has done the same.
    It has produced most cases many, many copies of each of media, and due to the many places that the media got switched to, some items need to be located to play.  Especially Movies.  None of the data is on cell phone, ipad or icloud.  It is mainly conflicts somewhere from files on my Macbook Pro and what is on my WD wireless hard drive. (I only save media on this hard drive)
    I have attempted to organize itunes, but it seems to perpetuate the problem.
    Is there anyone who knows the best way to find and consolidate media for itunes and place it in one folder.  Is there some App out there that deals ONLY with itunes media consolidating?
    Thanks,
    Tina C.

    What do you mean "retrieve"?
    If you mean previously purchased music, then you cannot, unless you are in the U.S. Then you can use the recently released icloud ( beta version).
    Tap itunes, then tap purchasd

  • Multiple classes in one file?

    I would like to do the following
    #import <UIKit/UIKit.h>
    @interface O : NSObject
    @end
    @interface A : O
    - (void) hi;
    @end
    @interface B : O
    - (void) hi;
    @end
    but the compiler complains. I do not want to create (O.h, O.m), (A.h, A.m), and (B.h, B.m) files. Surely one can declare multiple classes in a single file? If so, how. If not, why not?
    -Doug Danforth

    I've done that, should work. What is the compiler complaining about?
    Could it be the missing semicolons after the @end statements?

  • How to put multiple classes in a single file?

    Hello,
    I'd like to put mutliple classes in a single file. (This
    would be useful for grouping children that are minor extensions of
    parent classes or helper classes that are used by one class only).
    When I tried to put two classes in one file, I got this error
    message:
    5006: An ActionScript file can not have more than one
    externally visible definition: Notation.editField,
    Notation.labelField1
    This is the structure I used. Thanks in advance for your
    help.

    You can declare multiple classes in a single file, but only
    one can be
    within the package declaration. All class declarations
    outside the package
    are invisible to code outside the file.
    package sample
    public class SampleClass
    class SampleClassHelper
    class SampleClassHelper2

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

  • How many classes should a .java file contain?

    Are there any general guidelines on how many Java classes should be contained in a single .java file? I've been creating a new .java file for each class I write but see examples in Sun's Java tutorial where there are at least two classes in a single .java file. One such case is the TableDemo example at
    http://java.sun.com/docs/books/tutorial/uiswing/components/example-swing/TableDemo.java
    Thanks.

    The example you point to isn't really more the one class in one java file. It makes use of an inner class. An inner class is something different! As far as I understand it, it is mainly used to implement some behaviour (in this case AbstractTableModel behaviour). If the MyTableModel was in a separate class file, it couldn't access the variable of the main TableDemo class. So they use an inner class.
    So unless you need inner classes, this example is no reason to put more than one class in a .java file. In general, just put one class in a .java file. It's easier for everybody.

  • Putting a class of objects in a Linked List?

    Hi,
    I copied a program from a book and I want to edit it and put studentRecord class in the Linked List. I've tried to play about with datum, but everything I try doesn't work. Can someone help me out? How could I put studentRecord in the LinkedList?
    import java.io.*;
    class IO
         static BufferedReader keyboard = new
              BufferedReader(new InputStreamReader(System.in));
         static PrintWriter screen = new PrintWriter(System.out, true);
    class studentRecord
         private String name;
         private int IDNumber;
    class LinkedList
         class Node
              protected Object datum;
              protected Node link;
              public Node() {}
              public Node(Object item, Node pointer)
                   datum = item;
                   link = pointer;
         private Node head;
         private Node tail;
         private Node temporary;
         private int nodeCount = 0;
         //constructor
         public LinkedList()
              head = null;
              tail = null;
              temporary = null;
         //method to insert an object into the linked list
         public void insert(Object datum)
              if (head == null) // list empty
                   head = new Node(datum, head);
                   tail = head;
              else
                   temporary = new Node(datum, temporary);
                   tail.link = temporary;
                   tail = temporary;
                   temporary = null;
              nodeCount++;
    Full program can be found: http://dil3mma.tripod.com/LinkedList.txt
    Thanks in advance.

    Hi jverd,
    Thanks for replying. I've tried to change the program liked you said but there is 1 error I can't seem to fix(Im sure there are more tho). The error is "cannot resolve symbol" I typed in caps the line it happens on so it's easy to see. Any idea what it could be? Is it cause I'm comparing a String with Object?
    import java.io.*;
    class IO
         static BufferedReader keyboard = new
              BufferedReader(new InputStreamReader(System.in));
         static PrintWriter screen = new PrintWriter(System.out, true);
    class sRecord
         private String name;
         private int IDNumber;
    class LinkedList
         class Node
              protected sRecord datum;
              protected Node link;
              public Node() {}
              public Node(sRecord item, Node pointer)
                   datum = item;
                   link = pointer;
         private Node head;
         private Node tail;
         private Node temporary;
         private int nodeCount = 0;
         //constructor
         public LinkedList()
              head = null;
              tail = null;
              temporary = null;
         //method to insert an object into the linked list
         public void insert(sRecord datum)
              if (head == null) // list empty
                   head = new Node(datum, head);
                   tail = head;
              else
                   temporary = new Node(datum, temporary);
                   tail.link = temporary;
                   tail = temporary;
                   temporary = null;
              nodeCount++;
         //method to delete an object from the linked list
         public boolean delete(Object scrap)
              Node previous = head;
              //for every node in the linked list
              for (Node current = head; current != null; current = current.link)
                   //node to be deleted is at the head of the list
                   if (current.datum.equals(scrap) && previous == current)
                        head = current.link;
                        if (head == null) tail = null;
                        nodeCount--;
                        return true;
                   //node to be deleted is after the first node and before the last
                   else if (current.datum.equals(scrap) && (current.link != null))
                        previous.link = current.link;
                        nodeCount--;
                        return true;
                   //node to be deleted is at the ned of list
                   else if (current.datum.equals(scrap) && (current.link == null))
                        tail = previous;
                        previous.link = null;
                        nodeCount--;
                        return true;
                   previous = current;
              return false;
         //method to display the contents of a linked list
         public void displayList()
              Node temporary = head;
              if (head == null)
                   IO.screen.println("linked list is empty");
                   return;
              while (temporary != null)
                   IO.screen.println(temporary.datum);
                   temporary = temporary.link;
         //method to return true if the linked list is empty
         public boolean isEmpty()
              return (nodeCount == 0);
         //method to return the number of nodes in the linked list
         public int nodes()
              return nodeCount;
         //method to display a menu to insert data into the linked list
         static private char menu()
              char response = '\u0000';
              IO.screen.println("Do you want to ");
              IO.screen.print("nsert, [D]elete, [L]ist, [E]xit? ");
              IO.screen.flush();
              boolean done=false;
              do
                   try
                        String data = IO.keyboard.readLine();
                        response = Character.toUpperCase(data.charAt(0));
                        done = true;
                   catch (Exception e)
                        IO.screen.println("Please input a single character I, D, L or E");
              } while (! done);
              return response;
         static public void main(String[] args) throws IOException
              LinkedList list = new LinkedList();
              String datum;
              char choice;
              //get information from menu
              choice = menu();
              for (;;)
                   //Menu
                   switch (choice)
                        case 'I' :
                             IO.screen.println("type quit to finish input");
                             IO.screen.print("Enter a word ");
                             IO.screen.flush();
                             datum = IO.keyboard.readLine();
                             while (! datum.equals("quit"))
    THE ERROR HAPPENS HERE ON THIS LINE          list.insert(datum.name);
                                  IO.screen.print("Enter another word");
                                  IO.screen.flush();
                                  datum = IO.keyboard.readLine();
                             break;
                   case 'D' :
                        //if list is empty deletion not possible
                        if (list.isEmpty())
                             IO.screen.println("linked list is empty");
                             break;
                        IO.screen.println("type quit to finish input");
                        IO.screen.print("Delete? ");
                        IO.screen.flush();
                        datum = IO.keyboard.readLine();
                        while (! datum.equals("quit"))
                             if (list.delete(datum))
                                  IO.screen.println(datum+" was scrapped!");
                             //if list is empty deletion is not possible
                             if (list.isEmpty())
                                  IO.screen.println("linked list is empty");
                                  break;
                             IO.screen.print("Delete? ");
                             IO.screen.flush();
                             datum = IO.keyboard.readLine();
                        break;
                   case 'L' :
                        list.displayList();
                        IO.screen.println("number of nodes " + list.nodes());
                        break;
                   case 'E' : System.exit(0);
              //get information from menu
              choice = menu();

  • How to complie an application with many classes?

    I build an application with many classes,
    java palm.database.MakePalmApp -v -version "1.0" -icon 1.bmp -bootclasspath %j2meclasspath% -networking -classpath output %1
    this only works with only one class, I do not know if I have an application with several classes how can I convert them into a prc??

    Take a look at the mksample.bat file that came with
    midp4palm:
    java -jar MakeMIDPApp.jar -version 1.0 -type Data -creator mJav -nobeam -v -o ManyBalls_new.prc -JARtoPRC ManyBalls.jar example.manyballs.ManyBalls
    You will need to create a jar file consisting of the
    classes for your application, and then run it through
    a statement like above.
    Perry

  • How many classes in Java?

    I'm new to Java.
    Does anyone have a link or know the exact number of classes and methods in the java programming language that we use and their classifications (e.g. how many number of classes are used only for drawing rectangular objects on a form and the names of those classes)
    I need to see ALL the possible classes and categories they apply to.
    Is there a source I can access to view such information?
    Appreciated.

    wow.... now about that zipped file with so many class definitions, maybe an extra strap and a java programmer to sit on the thing to prevent it from exploding.
    thanx for your help people.
    Regards,
    Zeeshan

Maybe you are looking for

  • Export HDV timeline as AVI to edit in Premiere

    I have a finished 10 minute show show and edited in HDV in FCP 5.1.1. My client wants to use excerpts in a show they are editing in house. They use Premiere Pro 2.0 (latest release). So they have requested an AVI of the timeline in HDV resolution. I

  • Controlling time in Actionscript 3.0

    I was wondering if there is a way to directly control time in AS 3.0 I have some code such as: thumb1.addEventListener(MouseEvent.ROLL_OVER, scaleUP) function scaleUP(e:MouseEvent):void { thumb1.scaleX = thumb1.scaleY = 1.5 Obviously, this instantly

  • Photoshop CC unexpectedly terminates just after startup. Other Creative cloud applications starts normally. Any sugggestions?

    Photoshop CCunexpectedly just after startup. Other Creative cloud applications starts normally. I am running Windows 7 64 bit, GForce GTX670. Any sugggestions? I've tried re installing photoshop, tablet drivers, adobe cloude, and graphic drivers. I g

  • Button not showing up

    Hello, I'm moving an application from one apex instance to another. There is a template based button where the left and right sides of the image are showing up but not the center. Here is the template code: <table class="t3ButtonAlternative3" cellspa

  • Chromeless AIR app in FB4

    Hi all, I want a chromeless AIR app, i tried editing the xml file, i changed to <systemChrome>none</systemChrome> <transparent>true</transparent> still its not done totally, the background is still there, how to  remove or make it transparent??