Zach Latta
Zach Latta

Reputation: 3331

Multiple classes in single file

I'm having trouble putting multiple classes into a single file. For example, when my file looks like:

public class FirstClass() {}
public class SecondClass() {}
public class ThirdClass() {}

I get an error during compilation. I'm not quite sure what's causing this. Any ideas?

Upvotes: 10

Views: 59851

Answers (8)

Soban Jawaid
Soban Jawaid

Reputation: 1

There is no restriction on the number of classes in a java file.

But we can’t have more than one public class per source code file. Also the name of the file must match the name of the public class. Like, a class declared as public class Dog { } must be in a source code file named Dog.java.

And files with no public classes can have a name that does not match any of the classes in the file.

Upvotes: 0

Andy Thomas
Andy Thomas

Reputation: 86391

One Java file can contain at most one top-level public class. That public top-level class can contain any number of public nested classes.

You can eliminate your compiler errors by any of the following approaches:

  • Moving the other classes into their own files. For example: FirstClass.java, SecondClass.java, and ThirdClass.java.
  • Nesting the classes whose name is not the file basename. For example:

    public class FirstClass() {
        public class SecondClass() {}   
        public class ThirdClass() {}
    }
    
  • Removing the public qualifier from all but the one class whose name is the file basename. This approach has become less common after the introduction of nested classes in Java v1.1. For example, in file FirstClass.java, you could have:

    public class FirstClass() {}
    class SecondClass() {}   
    class ThirdClass() {}
    

From the Java Language Specification, section 7.6: Top Level Type Declarations:

If and only if packages are stored in a file system (§7.2), 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).

Upvotes: 1

Bharat Sinha
Bharat Sinha

Reputation: 14363

One Java file can consist of multiple classes with the restriction that only one of them can be public. As soon as you remove public keyword from your classes, you can combine them into a single Java file.

Upvotes: 24

Arun Chandravanshi
Arun Chandravanshi

Reputation: 62

Yes You can write your all classes in a single .java file, But you must have only one class public(if file name and class name same)

Example:

class A { }

class B { }

class C { }

Upvotes: 2

Yogendra Singh
Yogendra Singh

Reputation: 34367

I see you have already done that kind of implementation. Please refer

    private class CalcButtonListener implements ActionListener

in your TheaterWindow class.

By doing this, you are creating inner classes i.e. CalcButtonListener is an inner class of TheaterWindow class. Some concept you can extend to other classes.

Upvotes: 0

shriguru nayak
shriguru nayak

Reputation: 310

I am assuming you are very beginner! Just copy paste all these contents in a single file TheaterDemo.java. And dont forget to remove all the public keyword in the beginning of class declaration.

Upvotes: 1

Arun Manivannan
Arun Manivannan

Reputation: 4313

At the risk of spoon-feeding

Please read http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class TheaterWindow extends JFrame
{
    private JPanel pnlAdultTicketPrice, pnlAdultTicketsSold, pnlChildTicketPrice, pnlChildTicketsSold,
        pnlCalculate, pnlMain;
    private JLabel lblAdultTicketPrice, lblAdultTicketsSold, lblChildTicketPrice, lblChildTicketsSold;
    private JTextField txtAdultTicketPrice, txtAdultTicketsSold, txtChildTicketPrice, txtChildTicketsSold;
    private JButton btnCalculate;

    public TheaterWindow()
    {
        // Sets window title
        setTitle("Theater");

        // Sets layout to BorderLayout
        setLayout(new GridLayout(5,1));

        // Specifies what happens when close button is clicked
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Builds the panels
        buildPanels();

        // Add the panels to the frame's content pane
        add(pnlAdultTicketPrice);
        add(pnlChildTicketPrice);
        add(pnlAdultTicketsSold);
        add(pnlChildTicketsSold);
        add(pnlCalculate);

        // Size the frame to fit all of the panels
        pack();

        // Display the window
        setVisible(true);
    }

    private void buildPanels()
    {
        // Creates labels to display instructions
        lblAdultTicketPrice = new JLabel("Adult ticket price");
        lblChildTicketPrice = new JLabel("Child ticket price");
        lblAdultTicketsSold = new JLabel("Adult tickets sold");
        lblChildTicketsSold = new JLabel("Child tickets sold");

        // Creates text fields that are 10 characters wide
        txtAdultTicketPrice = new JTextField(10);
        txtChildTicketPrice = new JTextField(10);
        txtAdultTicketsSold = new JTextField(10);
        txtChildTicketsSold = new JTextField(10);

        // Creates button with caption
        btnCalculate = new JButton("Calculate");

        // Adds action listener to button
        btnCalculate.addActionListener(new CalcButtonListener());

        // Creates panels
        pnlAdultTicketPrice = new JPanel();
        pnlChildTicketPrice = new JPanel();
        pnlAdultTicketsSold = new JPanel();
        pnlChildTicketsSold = new JPanel();
        pnlCalculate = new JPanel();
        pnlMain = new JPanel();

        // Adds elements to their proper panels
        pnlAdultTicketPrice.add(lblAdultTicketPrice);
        pnlAdultTicketPrice.add(txtAdultTicketPrice);
        pnlChildTicketPrice.add(lblChildTicketPrice);
        pnlChildTicketPrice.add(txtChildTicketPrice);
        pnlAdultTicketsSold.add(lblAdultTicketsSold);
        pnlAdultTicketsSold.add(txtAdultTicketsSold);
        pnlChildTicketsSold.add(lblChildTicketsSold);
        pnlChildTicketsSold.add(txtChildTicketsSold);
        pnlCalculate.add(btnCalculate);

        // Adds all of the above panels to a main panel
        pnlMain.add(pnlAdultTicketPrice);
        pnlMain.add(pnlChildTicketPrice);
        pnlMain.add(pnlAdultTicketsSold);
        pnlMain.add(pnlChildTicketsSold);
        pnlMain.add(pnlCalculate);
    }

    private class CalcButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            // Creates object of Theater
            Theater theater = new Theater();

            // Sets the member variables of Theater to the user's input
            theater.setAdultTicketPrice(Double.parseDouble(txtAdultTicketPrice.getText()));
            theater.setChildTicketPrice(Double.parseDouble(txtChildTicketPrice.getText()));
            theater.setAdultTicketsSold(Integer.parseInt(txtAdultTicketsSold.getText()));
            theater.setChildTicketsSold(Integer.parseInt(txtChildTicketsSold.getText()));

            // Creates DecimalFormat object for rounding
            DecimalFormat dollar = new DecimalFormat("#.##");

            // Display the charges.
            JOptionPane.showMessageDialog(null, "Adult ticket gross: $" +
                    Double.valueOf(dollar.format(theater.getAdultGross())) + "\n" +
                    "Child ticket gross: $" + Double.valueOf(dollar.format(theater.getChildGross())) + "\n" +
                    "Adult ticket net: $" + Double.valueOf(dollar.format(theater.getAdultNet())) + "\n" +
                    "Child ticket net: $" + Double.valueOf(dollar.format(theater.getChildNet())) + "\n" +
                    "Total gross: $" + Double.valueOf(dollar.format(theater.getChildGross())) + "\n" +
                    "Total net: $" + Double.valueOf(dollar.format(theater.getTotalNet())));
        }
    }

    public class Theater
    {
        private double PERCENTAGE_KEPT = 0.20;

        private double adultTicketPrice, childTicketPrice;
        private int adultTicketsSold, childTicketsSold;

        public double getAdultGross()
        {
            return getAdultTicketPrice() * getAdultTicketsSold();
        }

        public double getAdultNet()
        {
            return PERCENTAGE_KEPT * getAdultGross();
        }

        public double getAdultTicketPrice()
        {
            return adultTicketPrice;
        }

        public int getAdultTicketsSold()
        {
            return adultTicketsSold;
        }

        public double getChildGross()
        {
            return getChildTicketPrice() * getChildTicketsSold();
        }

        public double getChildNet()
        {
            return PERCENTAGE_KEPT * getChildGross();
        }

        public double getChildTicketPrice()
        {
            return childTicketPrice;
        }

        public int getChildTicketsSold()
        {
            return childTicketsSold;
        }

        public double getTotalGross()
        {
            return getChildGross() + getAdultGross();
        }

        public double getTotalNet()
        {
            return getChildGross() + getChildNet();
        }

        public void setAdultTicketPrice(double adultTicketPrice)
        {
            this.adultTicketPrice = adultTicketPrice;
        }

        public void setAdultTicketsSold(int adultTicketsSold)
        {
            this.adultTicketsSold = adultTicketsSold;
        }

        public void setChildTicketPrice(double childTicketPrice)
        {
            this.childTicketPrice = childTicketPrice;
        }

        public void setChildTicketsSold(int childTicketsSold)
        {
            this.childTicketsSold = childTicketsSold;
        }
    }
}

Upvotes: 4

kosa
kosa

Reputation: 66637

Just remove public from all other class definition and paste the code into TheaterDemo.java file

public class TheaterDemo
{
    public static void main(String[] args)
    {
        TheaterWindow theaterWindow = new TheaterWindow();
    }
}
//Here class code after removing public

// Here another class code

Upvotes: 0

Related Questions