aspose file tools*
The moose likes Beginning Java and the fly likes Should new objects only be created in the application starting class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Should new objects only be created in the application starting class?" Watch "Should new objects only be created in the application starting class?" New topic
Author

Should new objects only be created in the application starting class?

Frank Bergers
Greenhorn

Joined: Feb 06, 2013
Posts: 7
Hi,

imagine the following scenario:

I' ve got three classes:

- one for starting the application (1)
- one for connecting to the database (2)
- one with getter- and setter-methods to work on a table in the connected database (3)

They are organized in the same package.

Where would I create instances of (2) and (3)? Would (1) be the right place to do so?
That means: should all instances generally be created in a starting class?
What are the pro' s and con' s to do this?
Or does it depend on the application itself which way' s the best to choose?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11475
    
  16

Generally, you would create the object when you need it.

The idea is to make your code as clean and simple as possible. You spend a LOT of time looking at old code, and you want to make it as easy as you can for your future self (or others) to understand what you are doing. Creating an object you don't use until way, way, way later can be very confusing.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Frank Bergers
Greenhorn

Joined: Feb 06, 2013
Posts: 7
Hi Fred,

I' ll rewrite my code based on your explanation.
Thanks for your help!
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1820
    
    7

There is a catch, though.
Following this approach in the example you provided might lead to (3) creating an instance of (2) to establish a connection to the database when you need to access a table.
It might well be better to have (1) create the instance of (2) and pass it to an instance of (3). That way you could keep track of a pool of database connections, or it would allow you to easily swap out an instance of (2) for a test double in a unit test.
This is called dependency injection and it directly impacts the responsibility of object creation.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Anna Depenbusch
Greenhorn

Joined: Dec 04, 2013
Posts: 1

The question is a very good one, and it touches one of the very topics of Java (or common OO) architecture. At the time when you are getting deeper into Java programming you will probably get used to principles and paradigms dealing with situations where you can do it "this or that way".

Regarding your question these may be:
  • Keep it simple.
  • Create objects where they are needed.
  • Use prooven patterns, like singleton or factory.
  • Use decoupling, i.e. let others do the object creation job (e.g. by dependency injection).

  • However, most important in your case:
  • Start with reasonable choices, be open to refactor (rewrite) it later.
  • Always think about what you're doing and why.

  • You're a Java rookie? Don't think about dependency injection or factories or singletons right now, start with the simple solution and learn later.
    Frank Bergers
    Greenhorn

    Joined: Feb 06, 2013
    Posts: 7
    Thanks, Jelle and Anna, I' ll keep this in mind when working on my code
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39773
        
      28
    welcome to the Ranch Anna Depenbusch
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Should new objects only be created in the application starting class?