• Post Reply Bookmark Topic Watch Topic
  • New Topic

utility class to load files from db: static or not?  RSS feed

 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say my web users need to load files out of the database, etc. So a servlet goes to retrieve a file by ID and asks a utility class to execute the SQL and return the file back to the servlet, so that the servlet can then hand that file to the user. Should I make that utility class static and have only one instance of it throughout the web app? Would that simplify things so that every session didn't have to store one more thing? I could just pass the login credentials to the utility class and it could check to see whether that person were allowed to access that file out of the database. Or would it actually be simpler to let every session have their own instance of such a class? Or would it not matter much either way in this case? :roll:
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should I make that utility class static and have only one instance of it throughout the web app? Would that simplify things so that every session didn't have to store one more thing?


Why would anything need to be stored in the session at all?

Your servlet (or better yet, business layer) could create and invoke an instance of this 'file manager' class without having to store it in the session.

I assume you have a business layer object or hierarchy that checks access rights for each user based upon login credentials that the file manager could use to check whether access should be permitted or not.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
P.S. In a more general sense, and in my opinion, the answer to the question 'should this class be static?' is always "no" unless you can write a 7-page dissertation justifying the violation of object orientation.
[ December 21, 2004: Message edited by: Bear Bibeault ]
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds like a good way to say it! So...avoid static classes unless it's one used for something like dynamic system config, right?
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stephen Huey:
That sounds like a good way to say it! So...avoid static classes unless it's one used for something like dynamic system config, right?


yeah we can use static for system config. i think its not about dynamic or static.

some folks say "dont use static at all, because its a violation to OO, use final instead."

actually we use static when need to load everything at compilation.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!