The public type classname must be defined in its own file error in Eclipse
If .java
file contains top level (not nested) public
class, it has to have the same name as that public class. So if you have class like public class A{...}
it needs to be placed in A.java
file. Because of that we can't have two public classes in one .java
file.
If having two public classes would be allowed then, and lets say aside from public A
class file would also contain public class B{}
it would require from A.java
file to be also named as B.java
but files can't have two (or more) names (at least in all systems on which Java can be run).
So assuming your code is placed in StaticDemoShow.java
file you have two options:
If you want to have other class in same file make them non-public (lack of visibility modifier will represent default/package-private visibility)
class StaticDemo { // It can no longer public
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 { // Only one top level public class in same .java file
public static void main() {
StaticDemo.show();
}
}Move all public classes to their own
.java
files. So in your case you would need to split it into two files:StaticDemo.java
public class StaticDemo { // Note: same name as name of file
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);
}
}StaticDemoShow.java
public class StaticDemoShow {
public static void main() {
StaticDemo.show();
}
}
Java compiler error: public type .. must be defined in its own file?
The file needs to be called DNSLookUp.java
and you need to put:
import java.net.InetAddress;
import java.net.UnknownHostException;
At the top of the file
The public type classname must be defined in its own file: bad style or nice feature
- simple answer: if its not a big deal to move those types to separate files each, then one should do it!
although its generally bad practice! (as shown e.g. here) to put multiple types into one "java container" file (and you may get there via Google like me :-) ), there are (rare) situations where this makes sense, e.g.
stuff rightfully contained in a package-info.java (like annotation types etc.) or
if you e.g. re-generate a lot of (likely similar in nature) types and for simplicity want to keep it in one file, e.g. if you create/keep-in-sync enums from database "keys" (like I have to).
Then you can just wrap those types by some "parent class", e.g.
/** do not edit! auto-generated if DB schema changes */
class MyKeys {
public enum EditStatusKey { ... }
public enum CustomerTypeKey { ... }
... // maybe many(!) other types following
}and access them via
MyKeys.EditStatusKey...
- keep in mind to maybe use
static
inner classes (e.g.public static class EditStatusKeys { ... }
if you are not using Enums (like in my case)
- keep in mind to maybe use
(just in case you doubt my example: the automation around my db-java-code sync logic would be unnecessary complicated to work on multiple files (there are more than 30 types auto-generated from a single table) and the benefit in the Java environment is not there and it may even be worse using it)
Type A is already defined error
Well, the first thing to check is obviously whether or not you have another class called A
in your file or in the same package.
How to use ClassT in Java?
Using the generified version of class Class allows you, among other things, to write things like
Class<? extends Collection> someCollectionClass = someMethod();
and then you can be sure that the Class object you receive extends Collection
, and an instance of this class will be (at least) a Collection.
Related Topics
Converting Decimal to Binary Java
Parallel Streams, Collectors and Thread Safety
Making Distinctions Between Different Kinds of Jsf Managed-Beans
How to Read Request Body Multiple Times in Spring 'Handlermethodargumentresolver'
How to Get a Value from the Last Inserted Row
Must Spring Component Classes Be Thread-Safe
How to Avoid 'Instanceof' When Implementing Factory Design Pattern
Strange Java Null Behavior in Method Overloading
Jsoup Character Encoding Issue
Having a 3Rd Party Jar Included in Maven Shaded Jar Without Adding It to Local Repository
How to Avoid Type Safety Warnings with Hibernate Hql Results
How to Add an Extra Source Directory for Maven to Compile and Include in the Build Jar
Difference Between Getclass().Getclassloader().Getresource() and Getclass.Getresource()
Hibernate Validation of Collections of Primitives
Differencebetween Double.Parsedouble(String) and Double.Valueof(String)
How to Properly Configure Implicit/Explicit Waits and Pageloadtimeout Through Selenium