Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Frank Bergers
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12122
30
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Frank Bergers
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fred,

I' ll rewrite my code based on your explanation.
Thanks for your help!
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anna Depenbusch
Greenhorn
Posts: 1
Eclipse IDE Spring Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks, Jelle and Anna, I' ll keep this in mind when working on my code
     
    Campbell Ritchie
    Sheriff
    Posts: 48930
    58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    welcome to the Ranch Anna Depenbusch
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic