简体   繁体   English

如何编译具有多个顶级类的单个Java文件

[英]How to compile single java file with multiple top level classes

This is my java code: 这是我的Java代码:

import java.io.*;
import java.util.*;
public class student
{
    String name;
    int age;
    float cgpa;
}
public class getdata
{
    public static void main(String args[])
    {
        int n;
        Scanner in=new Scanner(System.in);
        n=in.nextInt();
        student[] s=new student[n];
        for(int i=0;i<n;i++)
        {
            s[i]=new student();
            s[i].name=in.nextLine();
            s[i].age=in.nextInt();
            s[i].cgpa=in.nextFloat();
        }
        System.out.println();
        System.out.println("Name\tAge\tCGPA\n");
        for(int i=0;i<n;i++)
        {
            System.out.println(s[i].name+"\t"+s[i].age+"\t"+s[i].cgpa+"\n");
        }
    }
}

When I compile the program, it gives the error that each class should be in a different file. 当我编译程序时,它给出了每个类应位于不同文件中的错误。 How can i compile this without any error? 我该如何编译而没有任何错误? (I know this is not the best practice, but I need to do this in the same file.) (我知道这不是最佳做法,但是我需要在同一文件中执行此操作。)

The general rule is that you can have only one public class in a Java file. 一般规则是,Java文件中只能有一个公共类

If you want it to be visible from everywhere (this is based on your comment) you can declare the class as public static inside the getdata. 如果您希望它在任何地方都可见(这基于您的评论),则可以将类声明为getdata内部的public static。

It will be something like this: 将会是这样的:

public class getdata {
   public static class user {
     //class definition here
  }
}

Now your User class is available for instantiation everywhere. 现在,您的User类可在任何地方实例化。

Make the class Student to an inner class: 使班级学生进入内部班级:

import java.io.*;
import java.util.*;
public class getdata
{
    public static void main(String args[])
    {
        int n;
        Scanner in=new Scanner(System.in);
        n=in.nextInt();
        student[] s=new student[n];
        for(int i=0;i<n;i++)
        {
            s[i]=new student();
            s[i].name=in.nextLine();
            s[i].age=in.nextInt();
            s[i].cgpa=in.nextFloat();
        }
        System.out.println();
        System.out.println("Name\tAge\tCGPA\n");
        for(int i=0;i<n;i++)
        {
            System.out.println(s[i].name+"\t"+s[i].age+"\t"+s[i].cgpa+"\n");
        }
    }
    public static class student
    {
        String name;
        int age;
        float cgpa;
    }

}

File name must be getdata.java . 文件名必须是getdata.java And read about java naming convention. 并阅读有关Java命名约定的信息。 Class names should start with uppercase letter. 类名应以大写字母开头。

You can make an inner class as mentioned before, or if you don't need to access this class from outside this file then just remove the public tag. 您可以像前面提到的那样创建一个内部类,或者如果您不需要从该文件外部访问该类,则只需删除公共标记即可。 This will allow you to put multiple top level classes in a single file. 这将允许您将多个顶级类放在一个文件中。

You can create a .class file and then simply run class you want to compile 您可以创建一个.class文件,然后简单地运行要编译的类

javac FileName.java

java fileName_containsMainMethod

EDIT class cannot be public EDIT类别不能公开

eg Consider the following class is saved as name Test.java and contain Two classes under single Name Test.java 例如,考虑以下类以名称Test.java保存,并在单个名称Test.java下包含两个类

class A
{
public static void main(String[] a)
{
System.out.println("Hello from A");
}
}

class B
{
public static void main(String[] a)
{
System.out.println("Hello from B");
}
}

Now to run this you need to do following javac Test.java 现在要运行它,您需要执行以下javac Test.java

To get Output from Class A 从A类获得输出

java A

To get Output from Class B 从B类获得输出

java B

NOTE Remove public access modifier from class 注意class删除公共访问修饰符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM