Other answers provide good interface tips, and this is probably more than what you are looking for. However, for more information, no, you do not need to create a new .java file for each new class, there are alternatives . However, keep in mind that more classes are not necessarily bad. Your options ...
Nested class:
public class A { public/private class B { } }
Instances of B can access private variables of A, but cannot be built without an instance of A (this is an approach that is often used for button handlers, etc.)
Nested static class:
public class A { public/private static class B { } }
Instances of B cannot access private variables of A, but instance A is not required to create them. Instances of B can be created anywhere if B is declared public or only in methods A if B is declared private.
Anonymous class:
public class A { private void setupLayout() { ... button.addClickListener(new ActionListener() { public void actionPerfored(ActionEvent e) { handleClick(); } }); } }
This strange syntax creates a class that does not have a name and functions in the same way as a nested class (for example, it can access private variables). This is an alternative form of writing nested classes, which is sometimes shorter, but leads to a very strange syntax.
source share