• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Fred,

I' ll rewrite my code based on your explanation.
Thanks for your help!
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
Eclipse IDE Spring Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks, Jelle and Anna, I' ll keep this in mind when working on my code:)
     
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    welcome to the Ranch Anna Depenbusch
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic