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. 烂泥:高负载均衡学习haproxy之安装与配置

    本文由秀依林枫提供友情赞助,首发于烂泥行天下 有关高负载均衡的软件,目前使用比较多的是haproxy.nginx和lvs.下面我们就开始学习haprxoy这款软件. 一.haproxy介绍 以下开始介 ...

  2. 腾讯开放平台 IOS应用URL schema、Boundle ID填写 (含微博、微信)

    解释如下: qq比较麻烦点,需要两个 URL schemes 1.QQ+appID(appid原本是10进制的,需要转换16进制,网址:http://tool.oschina.net/hexconve ...

  3. [转]Zabbix 3.0 安装笔记

    Zabbix 3.0 只支持CentOS 7.0以上版本,所以先在虚拟机中安装好CentOS 7.0 x64,并设置好IP,允许虚拟机联网. 1.安装MySQL 从最新版本的linux系统开始,默认的 ...

  4. python抓取网页中图片并保存到本地

    #-*-coding:utf-8-*- import os import uuid import urllib2 import cookielib '''获取文件后缀名''' def get_file ...

  5. 使用TCMalloc的堆栈检查

    在前一篇译文<TCMalloc:线程缓冲的Malloc>详细讲解了TCMalloc的工作原理和特点,今天翻译<heap-checking using tcmalloc>,了解T ...

  6. 报表session与应用session常识普及

    1. 报表session与应用session 报表集成到项目中可能会有一个疑问就是系统应用和报表应用在一个web服务器下,那系统session和报表session是不是一个session呢?如果不是那 ...

  7. 大话设计模式C++版——工厂模式在COM中的典型应用

    上篇<大话设计模式C++版——抽象工厂模式>中,我们拯救世界未遂,留下小小的遗憾,本篇中我们将给出一个解决方案——COM组件技术,同时也顺便扯扯工厂模式在COM组件技术中的应用. 工厂模式 ...

  8. 有评论就是我最大的动力~MySQL基础篇完结(存储引擎和图形化管理工具)

    hi 今天登上来,发现竟然有了3个评论~~加油吧! 这周的计划其实远远没有达到,然后下周还有一大堆事情...那么...周末好好玩吧~ 今天试图完结MySQL的基础篇知识,小白变为大白? 1.MySQL ...

  9. position:absolute/relative/fixed小结

    1.绝对定位:position:absolute; 当一个div块的位置被定义为绝对定位absolute时,也就意味着它失去了文档流的位置,后面的文档流会紧跟着补上来接替它的位置.如果上下左右的绝对偏 ...

  10. Docker的单主机容器网络

    作者:杨冬 欢迎转载,也请保留这段声明.谢谢! 出处: https://andyyoung01.github.io/ 或 http://andyyoung01.16mb.com/ 本篇文章主要探索Do ...