• Post Reply Bookmark Topic Watch Topic
  • New Topic

Acceptable to Use Secondary Classes?  RSS feed

 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am building a simple RESTful server to get a view into a platform's resources (file systems, memory, cpu utilization, etc.). I want to cut-down on the number of files required per resource, so I have put models for representations in the resource java file (models are only used by the respective resource class). I tried making them inner-classes of the resource class, but that didn't seem to work with JAX-RS, so I made them secondary classes in the same file.

I know that it is functional, but i this an acceptable way to do this?


 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:I want to cut-down on the number of files required per resource

Why? You understand that even though you have one source file, the number of class files will still be the same, right?
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Understood -- I want to be able to reduce the overall number of source files by containing all the source for given resource to a single file.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On a side note, JAX-RS allows you to return something and you automatically get a 200 with the returned value as content. If you return void you automatically get a 204. So you can change your code to this:
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob - great tip.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for your original question. I guess there's no problem in it this way (I've used the same technique in a test class recently), but in most cases I wouldn't do it like this. I prefer to have each class in its own source file, or nested if appropriate.

One thing I just thought of regarding my previous piece of code - I'm not 100% sure that will work with non-public classes as return or parameter types. In fact, the response classes have become part of the public API and should therefore themselves be public - even if the compiler and container allow non-public classes, it's not clean, and it would reflect in generated javadoc.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!