Results 1 to 6 of 6
- 12-14-2010, 10:09 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 17
- Rep Power
- 0
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
- 12-14-2010, 10:13 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 787
- Rep Power
- 11
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.
- 12-14-2010, 10:33 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 17
- Rep Power
- 0
<<you could use dependency injection frameworks like google guice ,spring, ejb and so on. >>
Could you clarify the above with an example?
- 12-14-2010, 10:50 PM #4
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 787
- Rep Power
- 11
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 :)
- 12-15-2010, 01:31 AM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 952
- Rep Power
- 10
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-
- 12-15-2010, 10:09 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 13,541
- Rep Power
- 26
Similar Threads
-
Making a netbeans project into a executable jar file
By counterfox in forum New To JavaReplies: 3Last Post: 12-03-2010, 06:09 PM -
Problem to access data when a class calls another class
By ea09530 in forum New To JavaReplies: 0Last Post: 04-04-2010, 11:06 AM -
java project help, reading in from a file and adding data to JTable
By Ekul in forum New To JavaReplies: 0Last Post: 11-24-2009, 02:49 PM -
Array project for class
By circuspeanuts in forum New To JavaReplies: 7Last Post: 04-17-2009, 01:44 AM -
Making variables in a class accessible to all once changed
By int80 in forum New To JavaReplies: 13Last Post: 08-23-2008, 10:09 PM
Bookmarks