Results 1 to 6 of 6
  1. #1
    JOHNINALBANY is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default Making Data available to every class in a project.

    I am new to java.

    I have a java class (GetData) that imports and processes data from a database. This data is used by other classes in my program. The GetData class is invoked by the main class of my program.

    In order to make these data available to every single class I am using the following approach: Every time an instance of a class is created, an instance of the GetData class is passed to the invoked class through the constructor.

    Is this the right approach to accomplish this? Are there any other solutions?

    I come from procedural programming where external variables can be used to accomplish this.Java does not have such a thing.

    Thanks

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    that`s right! but often it`s very hard to do that by hand so you could use dependency injection frameworks like google guice ,spring, ejb and so on.

  3. #3
    JOHNINALBANY is offline Member
    Join Date
    Nov 2010
    Posts
    17
    Rep Power
    0

    Default

    <<you could use dependency injection frameworks like google guice ,spring, ejb and so on. >>

    Could you clarify the above with an example?

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    you will find examples on the homepages of the frameworks, e.g. here: Motivation - google-guice - Project Hosting on Google Code

    but if you are new to java you should stay at your idea :)

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    If the data is read-only (sort of implied by the class name) then you might consider a Singleton pattern.

    Singleton pattern - Wikipedia, the free encyclopedia

    Basically, you create only one instance of the GetData object, and pass a reference to that one instance to all the classes that need it.

    -Gary-

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,922
    Rep Power
    19

    Default

    You don't need to pass the reference, though, for a Singleton since any class should be able to access the MySingleton.getInstance() method.

Similar Threads

  1. Making a netbeans project into a executable jar file
    By counterfox in forum New To Java
    Replies: 3
    Last Post: 12-03-2010, 05:09 PM
  2. Replies: 0
    Last Post: 04-04-2010, 10:06 AM
  3. Replies: 0
    Last Post: 11-24-2009, 01:49 PM
  4. Array project for class
    By circuspeanuts in forum New To Java
    Replies: 7
    Last Post: 04-17-2009, 12:44 AM
  5. Replies: 13
    Last Post: 08-23-2008, 09:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •