File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Objects" Watch "Objects" New topic
Author

Objects

R Dom
Greenhorn

Joined: Sep 08, 2010
Posts: 19
what is a good practice:

creating object when necessary or using same instance everywhere? how to decide when to use such practice.

Cheers,
RD
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 147

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).


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
James Elsey
Ranch Hand

Joined: Dec 21, 2007
Posts: 228

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


Kind Regards, James. OCPJP 1.6 || My SCJP / OCJCP Study Notes
Interested in : SCJP, Google App Engine, Stripes, Android;|| My Bite-Size SCJP Study Blog
R Dom
Greenhorn

Joined: Sep 08, 2010
Posts: 19
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

Joined: Jan 05, 2010
Posts: 64
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


Don't walk as if you rule the world, walk as if you don't care who rules it...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
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

Joined: Mar 17, 2010
Posts: 1051

Welcome to JavaRanch


SCJP6.0,My blog Ranchers from Delhi
Soniya Ahuja
Ranch Hand

Joined: Jul 20, 2008
Posts: 83
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


SCJP 1.5 | SCWCD 5 | SCJP 6.0
[url]http://a2zjava.webs.com[/url] - Online training for Java/JSPs and Servlets/SCJP/SCWCD
http://soniyaahuja.webs.com
 
wood burning stoves
 
subject: Objects