调试记录:The public type <<classname>> must be defined in its own file

This question already has an answer here:

I have written the following code:

package staticshow;


public class StaticDemo {
  static int a = 3;
  static int b = 4;

  static {
    System.out.println("Voila! Static block put into action");
  }

  static void show() {
    System.out.println("a= " + a);
    System.out.println("b= " + b);
  }
}

public class StaticDemoShow {
  public static void main() {
    StaticDemo.show(); 
  }
}

I am getting the error message:

The public type StaticDemo must be defined in its own file

error in the very first line public class StaticDemo {. Why is it happening and how can I resolve it? Note that my project name is StaticDemoShow, package name is staticshow and class names are as given in the code.

EDIT- After making just one class public or both the classes default, I am getting the error "Selection does not contain a main type". Now what should I do?

share improve this question

marked as duplicate by HolgerDennis MengRaedwaldDavid LJarrod Roberson Nov 6 '13 at 22:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

 
 
Is your class file named StaticDemo? –  Vaughan Hilts  Nov 6 '13 at 14:30 
 
@VaughanHilts, No. My project name is "StaticDemoShow". –  Mistu4u  Nov 6 '13 at 14:34
2 
No need to downvote IMHO. Every beginner will stumble across this. @VaughanHilts It seems both are in the same file. –  Axel  Nov 6 '13 at 14:34
 
duplicate of Java compiler error: "public type .. must be defined in its own file" and Public class must be defined in its own file and a lot more I guess… –  Holger  Nov 6 '13 at 19:14 
 
just in case you need to rightfully have mulitple types in one file, you may get here and look for another answer and motivation provided here: stackoverflow.com/questions/32012525/… –  Andreas Dietrich  Aug 14 '15 at 14:33 

8 Answers

up vote 15 down vote accepted

We cant have two public classes in one file. The JVM cannot understand, in one file we must write one public class only.

public class StaticDemo {

    static int a = 3;
    static int b = 4;

    static {
        System.out.println("Voila! Static block put into action");
    }

    static void show() {
        System.out.println("a= " + a);
        System.out.println("b= " + b);
    }

}
 class StaticDemoShow {
    public static void main() {
        StaticDemo.show();
    }

}
share improve this answer
在C++中,直接用`std::string className`存储类名并用`std::make_shared<className>()`来创建该类的新实例通常不是直接可行的,因为`std::make_shared`需要显式指定具体的类类型(如`std::make_shared<MyClass>()`),而不是通过字符串动态查找。 然而,如果你有一个预先存储了类名的列表(比如在一个字符串数组或map中),并且你想动态地根据这些名称创建类的实例,你需要借助反射机制,这在C++标准库中并没有提供直接的支持。可以考虑使用第三方库如Boost.Python或者第三方工具来间接实现。 一个简化版本的示例(假设我们有一个全局映射来存储这些信息): ```cpp #include <unordered_map> #include <typeinfo> // 假设有一个映射存储了类名和对应的构造函数字符串 std::unordered_map<std::string, std::string> classMap = {{"MyClass", "std::make_shared<MyClass>()"}}; // 获取类名 std::string className = "MyClass"; // 验证映射中是否存在这个类名 if (classMap.find(className) != classMap.end()) { // 使用eval函数尝试解析字符串为一个表达式,然后执行 auto factoryExpression = classMap[className]; auto sharedObject = evalAndCreateSharedObject(factoryExpression); // 这里需要自定义eval函数 } void evalAndCreateSharedObject(const std::string &expression) { // 这部分通常涉及更复杂的解析和执行过程,这里只是示意 try { auto factory = eval(expression); return factory(); // 假设工厂函数返回std::shared_ptr } catch(...) { throw std::runtime_error("Failed to create object from expression"); } } ``` 注意:实际使用时,这种做法可能存在安全风险,因为它涉及到字符串到C++代码的转换,如果恶意用户能够控制这些字符串,可能会引发严重的问题。因此,反射和代码执行应在严格的限制和安全措施下进行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值