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. Java api 入门教程 之 JAVA的SYSTEM类

    System类代表系统,系统级的很多属性和控制方法都放置在该类的内部.该类位于java.lang包. 由于该类的构造方法是private的,所以无法创建该类的对象,也就是无法实例化该类.其内部的成员变 ...

  2. react native windows开发环境搭建(一)

    ReactNative分为服务器端和手机端loader程序,Android版有3种代码:js代码,java代码和c++代码,主要是编写的是js代码,如果框架功能不足就需要编写原生的java代码来扩展, ...

  3. SpringMvc-Httl-shiro的整合

    来到新的公司一个月,以前实习公司的用的是srping+hibernate+struts2,而在这里不在用的这些了,而是用的springMVC和jdbc模板来操作数据了,所以又用了一段时间去慢慢融入这个 ...

  4. Javascript字数统计

    字数统计功能,原理是给textarea添加onKeyup事件,事件读取textarea内容并获得长度,并赋值给统计字数的那个文本节点,这里有一点要注意的是添加onKeypress和onKeydown事 ...

  5. Configure Ocserv on CentOS 6

    Configure Ocserv on CentOS 6 Table of Contents 1. Install ocserv 2. Configure ocserv 3. How to host ...

  6. Server Tomcat v7.0 Server at localhost failed to start.临时解决办法

    错误名:Server Tomcat v7.0 Server at localhost failed to start. 解决办法:去掉下面这句话: (通常在代码开头部分,public class前) ...

  7. ubuntu编译最新版本WebKit

    好久都没更新webkit 源码在ubuntu上编译了,网上搜了一下,基本上都是早期编译的webkit版本.可能是大家都去搞高大上的谷歌浏览器了吧. 今天就以ubuntu14.04版本作为编译环境来讲讲 ...

  8. Nova 操作汇总(限 libvirt 虚机) [Nova Operations Summary]

    本文梳理一下 Nova 主要操作的流程. 0. Nova REST-CLI-Horizon 操作对照表 Nova 基本的 CRUD 操作和 extensions: # 类别 Nova V2 REST ...

  9. Binary Tree: Write a function to return count of nodes in binary tree which has only one child.

    June 8, 2015 我最喜欢的一道算法题目, 二行代码. 编程序需要很强的逻辑思维, 严密,我还没有很好训练自己.想一想, 二行代码, 五分钟就可以搞定; 最近这几天网上大家热议的 Homebr ...

  10. jmeter 与 json

    1.添加一个 http信息头管理: Add new value for Name = “Content-Type” and value = “application/json” (without qu ...