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?

asked Nov 6 '13 at 14:29
Mistu4u

1,31751948

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
   
   
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

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();
} }

调试记录:The public type <<classname>> must be defined in its own file的更多相关文章

  1. android The public type classname must be defined in its own file 报错

    The public type classname must be defined in its own file classname  为类名 错误提示,公用的类必髯有自己拥有独立.java文件 解 ...

  2. Java报错 -- The public type c must be defined in its own file

    出现The public type c must be defined in its own file这个问题,是由于定义的JAVA类同文件名不一致 你的文件里很可能有两个 public 的类,而Ja ...

  3. 【转】JAVA错误:The public type *** must be defined in its own file***

    出现The public type xxx must be defined in its own file这个问题,是由于定义的JAVA类同文件名不一致.public类必须定义在它自己的文件中. 解决 ...

  4. 基于freescale i.Mx6(ARM)的阿里云oss调试记录

    交叉编译阿里OSS调试记录 1.1 开通oss服务 具体参考以下链接: https://help.aliyun.com/document_detail/31884.html?spm=a2c4g.111 ...

  5. [未完] Linux 4.4 USB —— spiflash模拟usb大容量存储设备 调试记录 Gadget Mass Stroage

    linux 4.4 USB Gadget Mass Stroage 硬件平台: licheepi nano衍生 调试记录 驱动信息 │ This driver is a replacement for ...

  6. SPI 核软件调试记录

    SPI 核软件调试记录 1.首先说说int SpiFlashWaitForFlashReady(void)这一函数,基本上其它函数在执行的时候,都会事先执行一次此函数.    因为此函数的作用主要是用 ...

  7. Video Timing Controller v6.1软件调试记录

    Video Timing Controller v6.1软件调试记录 GUI配置: . case XVTC_VMODE_PAL: //576i@50 { TimingPtr->Interlace ...

  8. Video Test Pattern Generator(7.0)软件调试记录

    Video Test Pattern Generator(7.0)软件调试记录 . XVidC_VideoMode XVIDC_VM_576_50_I = XVIDC_VM_720x576_50_I ...

  9. MA82G5D16AS16 主频调试记录

    MA82G5D16AS16 主频调试记录 当 SCKS 设置 为 MCKDO / 128 时 MCU 的电流为 0.58mA,100UF 电容可以维持 0.5S,大概可以满足. 但是需要注意外围的线路 ...

随机推荐

  1. DataTable转换为Json格式

    private string DataTableToJson(DataTable dt) { string Json = ""; Json = Json + "[&quo ...

  2. oops call trace 解析

    Call Trace: [  221.634988]  [<ffffffff8103fbc7>] ? kmld_pte_lookup+0x17/0x60 [  221.635016]  [ ...

  3. 使用jquery合并表格中相同文本的相邻单元格

    一.效果 二.代码 <!DOCTYPE HTML> <html> <head> <title>Example</title> <met ...

  4. [转]SQL Server 高性能写入的一些总结

    本文转自:http://www.cnblogs.com/rush/archive/2012/08/31/2666090.html 1.1.1 摘要 在开发过程中,我们不时会遇到系统性能瓶颈问题,而引起 ...

  5. [转]net中哈希表的使用 Hashtable

    本文转自:http://www.cnblogs.com/gsk99/archive/2011/08/28/2155988.html 以下是PetShop中DBHelper中的使用过程: //创建哈希表 ...

  6. UART Explained(转载)

    做嵌入式开发,UART几乎是必不可少的,调试串口.GPS.GPRS.Bluetooth等模块很多都是用的UART接口.时下火热的IoT也不乏UART的身影,串口的BLE.WIFI.Zigbee.Lor ...

  7. 图的全局最小割的Stoer-Wagner算法及例题

    Stoer-Wagner算法基本思想:如果能求出图中某两个顶点之间的最小割,更新答案后合并这两个顶点继续求最小割,到最后就得到答案. 算法步骤: --------------------------- ...

  8. 网络之TCP/IP四层模型

    应用层(应用层.表示层.会话层):FTP.Telnet.DNS.SMTP 传输层(传输层):TCP<传输控制协议> . UDP<用户数据报文协议> 网际互连层(网络层):IP. ...

  9. Excel实用操作

    目地 不能熟练操作Excel的程序员不是好策划. 一片区域填充相同数据 1.用鼠标框选一片区域,松开鼠标,不要点其它单元格 2.直接输入输入数据,输完之后,按Ctrl+Enter,选中的区域就会填充相 ...

  10. 入门训练 Fibonacci数列

      入门训练 Fibonacci数列   时间限制:1.0s   内存限制:256.0MB 问题描述 Fibonacci数列的递推公式为:Fn=Fn-1+Fn-2,其中F1=F2=1. 当n比较大时, ...