File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Objects

 
R Dom
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is a good practice:

creating object when necessary or using same instance everywhere? how to decide when to use such practice.
 
Jan Hoppmann
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd say both. Create the object when it's used (so it doesn't use memory before), and try to use the same object as often as possible (if no new object for new data is needed).
 
James Elsey
Ranch Hand
Posts: 228
Android IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Entirely depends on your situation.

You should create multiple instances of an object if they are likely to have different state. If you have an object that is always going to have the same state, you could look at statics
 
R Dom
Greenhorn
Posts: 19
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys for replying.

I am developing one application for practicing..

I have classes like MainUI , Dbconnection , FileValidator , DataImporter , QueryManager

Main functionality is importing data from excel files into the database.

I am creating the dataimporter object if file is valid and pass the each record to the querymanager which inserts record in db.

there are 2 different data importers and they share same queryManager object.

Is it a good idea to use same object or should i use different objects for them.
 
Ninad Kuchekar
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Objects have to be considered in real-time scenarios. They have State, Behavior, Identity & Responsibility.
Since you have given a brief idea on what you want to do, I would suggest to break up your system into smaller parts and identify the actors in the system. When I say actors think of them as live beings or in a more sophisticated language, personify them.

From what you said, I guess you can use the same queryManager object, or else you can use static-synchronized methods for database operation, again completely depends upon your scenario.


-Ninad
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think we can tell from what you have said whether you want several objects or one object used repeatedly.

Have a look through all the methods in your classes, and see whether there are any methods which do not query access or use any information from the object. Then see whether any of those methods do not alter record or enter any information into the object. You may do well to convert those methods to static methods, which belong to the class, and are best called without an object being created at all.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch
 
Soniya Ahuja
Ranch Hand
Posts: 83
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some more issues which you need to consider when you are using same objects and you are also doing database operations here. So while updating, etc. you might be faced with synchronization issues. Consider these while designing your application - though your application might seem safe, at some point it might break and you'd wonder what went wrong
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic