0

New class in Eclipse

When I create a new class using the above menu ,a new java file is created for each class and as you can see this leads to a lot of .java files very quickly(Why does ellipse show a .class structure inside a .java file, ) , is this a good design practice considering the fact that sometimes I only want classes to be very small such as

class Name
{
String firstName;
String lastName;
}

I'm new to Eclipse and Java IDE's , can you also tell me a shortcut to create a new class .

Sainath S.R
  • 3,074
  • 9
  • 41
  • 72
  • to create a new class in the same file you can use the buttons c l a s s ;-) – Henry Dec 06 '14 at 06:32
  • Sorry ,can you be more clear? – Sainath S.R Dec 06 '14 at 06:36
  • 2
    I've tried to maintain java programs where people have tried to stuff as many classes into a single file as possible, they're much harder to read. You're on the right track with one file per class (and the occasional inner class where it makes sense) – Richard Tingle Dec 06 '14 at 07:51

1 Answers1

5

It is not only good practice, it is the way Java was intended to be written.

Why is each public class in a separate file?

Multiple classes in the same file is possible, with nested classes and anonymous classes and so on, but you should really have a good reason to do such a thing. There is nothing wrong with a small class, and it greatly improves readability when you are not searching through large files looking for internal classes.

Community
  • 1
  • 1
TheMrSean
  • 66
  • 2