Multiple classes in one file

I find it difficult to put several classes in one file. For example, when my file looks like this:

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

During compilation, I get an error message. I'm not quite sure what this causes. Any ideas?

+8
source share
8 answers

A single Java file may consist of several classes with the restriction that only one of them may be publicly available. Once you remove the public keyword from your classes, you can merge them into a single Java file.

+21
source

At 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 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 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; } } } 
+4
source

Yes, you can write all classes in one .java file, but you should have only one public class (if the file name and its name are the same)

Example:

class A {}

class B {}

class C {}

+2
source

I assume you are very new! Just copy all this content into one TheaterDemo.java file. And don't forget to remove all public keywords at the beginning of the class declaration.

+1
source

A single Java file can contain at most one top-level public class. This top-level public class can contain any number of public nested classes.

You can fix your compiler errors in any of the following ways:

  • Moving other classes to your own files. For example: FirstClass.java, SecondClass.java and ThirdClass.java.
  • Nesting classes whose name is not a file name. For example:

     public class FirstClass() { public class SecondClass() {} public class ThirdClass() {} } 
  • Removing the public qualifier from all but one class whose name is a file name. This approach became less common after introducing nested classes in Java v1.1. For example, in the file FirstClass.java you can:

     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 the file system (ยง7.2), the host system may choose to force the restriction that it is a compile-time error if the type is not found in the file under the name consisting of the type name plus extension (for example, .java or .jav) if one of the following conditions 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 open (and therefore potentially available from code in other packages).

+1
source

Just remove the publication from all other class definitions and paste the code into the 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 
0
source

I see you have already made such an implementation. Please contact

  private class CalcButtonListener implements ActionListener 

in your TheaterWindow class.

This way you create inner classes, i.e. CalcButtonListener is an inner class of the TheaterWindow class. Some concept that you can extend to other classes.

0
source

There is no limit to the number of class files in a java file.

But we cannot have more than one open class for source code. Also, the file name must match the name of the public class. For example, a class declared as an open class Dog {} should be in a source code file named Dog.java.

And files without public classes may have a name that does not match any of the classes in the file.

0
source

All Articles