Linked Questions
34 questions linked to/from Java: Multiple class declarations in one file
-2 votes
1 answer
4k views
Do different classes need to be in different java files? [duplicate]
I am fairly new to Java, and whenever we have written programs in school, we put each class in a separate java file, but I do not remember why this was (if there was even a reason). Right now I am ...
1 vote
4 answers
2k views
Can I use multi java class in a same file? [duplicate]
I am a Java learner and while I was studying, I realized a book uses 2 or 3 classes in a same file. But when I actually followed the book's codes I was not able to run it. for example, Class Point {...
-2 votes
3 answers
181 views
Design Pattern within a java class [duplicate]
Possible Duplicate: Java: Multiple class declarations in one file Recently I was going through a java class in which I have discovered that it was a java class with the named A but after that ...
1 vote
1 answer
108 views
Inner class in class file but not in class body? [duplicate]
In the code of a legacy application I just came across a class file that contained inner classes, but those inner classes where not defined within the class body but after the closing bracket of the ...
0 votes
1 answer
153 views
What happens when you declare two or more classes inside one .java file with the file name same as the public class in it? [duplicate]
Does it creates individual .class files when it gets compiled? Is it something right to do?
-1 votes
1 answer
83 views
which type of class "filepanel" in code below? [duplicate]
in this code the classwhat we called class "filepanel" in below code.it is neither static inner nor inner then what we called it?i want to check for such type of classes from java code using AST ...
17 votes
9 answers
16k views
Why only one class per file [duplicate]
Still coming from C++ I find it cumbersome to create many small helper classes and objects. Mostly because I have to have one file per class. I basically have a class like this: public class ...
17 votes
6 answers
22k views
Java public interface and public class in same file
In a single .Java file, is it possible to have a public interface and a public class (which implements the interface) I'm new to Java coding and it is written on most places on the net that a .java ...
20 votes
3 answers
18k views
Multiple records inside the same .java file
We can always have multiple classes inside a .java file. Considering encapsulation and that each class consists of multiple lines of code it always made sense to have 1 class (don't talk about nested ...
20 votes
4 answers
7k views
Non-public top-level class vs static nested class
It seems to me that non-public top-level classes and static nested classes essentially perform the same tasks when creating a helper class. A.java public class A { public static main (String[] ...
8 votes
4 answers
18k views
How can I create a new class instance from a class within a (static) class?
I'm new to Java (have experience with C#), this is what I want to do: public final class MyClass { public class MyRelatedClass { ... } } public class OtherRandomClass { public ...
2 votes
3 answers
4k views
Should I put my classes inside or outside the main class? (Best Practices)
I'm just starting with Java, although I already have some experience with OOP in PHP In this example, I have a simple class called Person, with fields such as name, age, and sex. I was wondering what ...
0 votes
3 answers
3k views
How to throw exception in static method, both in same class
Example1Exception and Example1Method belong together in the same file. It would not make sense to put them in separate files. public class Example1 { public class Example1Exception extends ...
0 votes
1 answer
22k views
Error .java:23: error: class, interface, or enum expected import java.util.*;
I am not sure why I get continuing told the error of java22 in my code. I searched other person's question like around. like here Compiler error: "class, interface, or enum expected" But I ...
0 votes
2 answers
7k views
declare multiple class using Eclipse
is there a way to declare 2 classes on the same .java file using Eclipse - also how the compiler will distinguish the .class for each declare class. example public class ThisTest { public static ...