Class declaration in Java [duplicate]

3

Can I declare more than one class in the same file in Java, and if this is possible, is the most correct way to program in this language?

    
asked by anonymous 29.04.2018 / 14:38

2 answers

4

Yes, it is perfectly possible and it makes sense if classes are closely related, because only one of the classes can be public.

In this kind of thing it is not correct without seeing the context. There are those who recommend putting one class per file, and is even a good recommendation, but not that it should always be followed. Let's say it's not common, but it can when other classes are not as important.

    
29.04.2018 / 14:54
1

Yes it is possible, but it should be used with care, especially if you think about maintaining it, imagine that as the project grows these classes can change their scope and in practice, you (or other remember to modify this structure.

I suggest reading of this article on cohesion and coupling, this will help you in time to model their classes.

So you can use it, but you should analyze if it really makes sense for your project.

    
30.04.2018 / 03:17