aspose file tools*
The moose likes Java in General and the fly likes Which is the better approach Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Which is the better approach" Watch "Which is the better approach" New topic
Author

Which is the better approach

Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228
Hi,

I am writing a generic utility class. I have two approaches and wanted to know which is the best

Approach 1:



Client code to call util methods:



Approach 2:



Client code to call util methods:



If i use Approach 2 i cannot restrict client from calling Util util = new Util(); util.perform();

What are the implications of each approach?

Thanks,
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
You just have to add a private constructor to the second approach.

As for the restrictions of approach two: You will never be able to change your implementation to use multiple instances, but since this is a Utility class, this should be okay.


JDBCSupport - An easy to use, light-weight JDBC framework -
Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228
Should i make the utility class Util as final?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Srikanth Reddy wrote:Should i make the utility class Util as final?

"Should" you? I don't know--do you want it extensible?
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
If you are worried that the extending class could take the no-instance thing away, no worries... If your constructor is private it will never be possible to instantiate the class, even if another class extends it (well, using reflections,but that's a different story)...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

But yeah, you should make your class final to make it even clearer you can't extend it. Utility classes like this are in general final.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Which is the better approach