今天我在调试一个java程序时出错,出现提示class CountInstances is public,should be declared in a file named CountInstances.java;
并且显示指出public class CountInstances中的class出错,请问这是什么问题?
源代码如下:
public class CountInstances{
private static int numInstances =0;
protected static int getNumInstances(){
return numInstances;
}
private static void addInstance(){
numInstances++;
}
CountInstances(){
CountInstances.addInstance();
}
public static void main(String arguments []){
System.out.println("Starting with "+CountInstances.getNumInstances()+" instances");
for(int i=0;i<10;++i)
new CountInstances();
System.out.println("Created"+CountInstances.getNumInstances()+" instances");
}
}
并且显示指出public class CountInstances中的class出错,请问这是什么问题?
源代码如下:
public class CountInstances{
private static int numInstances =0;
protected static int getNumInstances(){
return numInstances;
}
private static void addInstance(){
numInstances++;
}
CountInstances(){
CountInstances.addInstance();
}
public static void main(String arguments []){
System.out.println("Starting with "+CountInstances.getNumInstances()+" instances");
for(int i=0;i<10;++i)
new CountInstances();
System.out.println("Created"+CountInstances.getNumInstances()+" instances");
}
}
解决方案 »
- C:\%e6%96%b0%e5%bb%ba\UTF-8\xxxx.txt 这是什么编码?怎么转换成中文字符串?
- 关于创建对象的问题,和创建数组对象!!求高手,小弟请教!!!
- 求一算法,欢迎大家积极讨论
- 求xml转MODEL函数
- 以空格为要求,按要求输出一段字符
- java内部类的问题
- String(byte[] bytes, int offset, int length) 这个构造方法里面的参数是什么意思
- windows 的系列产品(如 xp、2000)哪些自带能运行 applet 的 jvm 环境?
- JTable刷新?
- 请教关于void类型返回值的问题。
- 为什么"重写的方法不能比被重写的方法具有更严格的访问权限"
- 创建一个文本文件,将下面一段文字写入该文件中,读出该文件的数据并打印:
Starting with 0 instances
Created10 instances
这是输出结果
一楼的对,可能原因是你的源文件名和你的源文件里的修饰为public的类名相同
Starting with 0 instances
Created10 instances
二 如果类的名称定义的不是CountInstances,如果是在eclipse下面编辑,编辑器马上就会报错的,报错如下:
Multiple ers at this line
- The public type ss must be defined in its own file
- The public type y must be defined in its own file
如果使用的是记事本或者其他编写的,在dos命令行运行的话就会报错,还有就是要制定包路径