Results 1 to 5 of 5
- 10-22-2009, 06:01 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 50
- Rep Power
- 0
Accessing non-static public variables from another class
I'm sorry if this is a completely ridiculous question but this is the New To Java forum so here it goes.
I have instantiated public variables in my Main classJava Code:public class Dashboard_Main { public ResultSet rs; public DB_Connection connection; }
I can't figure out how to access these variables from my Dashboard_GUI class. I have a method to execute a sql command and then it will add the contents to a table in my GUIJava Code:public class Dashboard_GUI extends JFrame { private void setTableInfo(Object cbContents) { rs = connection.createQuery("SELECT * FROM dashboard_data.families_products WHERE depot='" + strDepotSelected + "'"); } }
- 10-22-2009, 06:07 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
rs and connection are members of an object of Dashboard_Main type (class). So you need an instantiation of that class, e.g. Dashboard_Main dm= new Dashboard_Main() to access those members, e.g. dm.rs and dm.connection.
kind regards,
Jos
- 10-22-2009, 06:18 PM #3
Member
- Join Date
- Dec 2008
- Posts
- 50
- Rep Power
- 0
So I create a Dashboard_Main() object in my Dashboard_GUI class, I get that now.
Now, if I were to change the DB_Main instantiation to:
DB_Connection connection = new DB_Connection(strURL);
thus actually connecting to the DB, would my Dashboard_GUI class be able to access the database through that connection or would I have to create a seperate connection for each class? That is what I'm trying to avoid, I don't want more than one connection per application that is open. Thanks for your help.
EDIT: OK, so rather than wasting your time I did a little trial and error and found that I can't use that in the initial instantiation because I need to catch a SQL error. Does anyone know a way to use one connection in more than one class?Last edited by ribbs2521; 10-22-2009 at 06:33 PM.
- 10-22-2009, 06:33 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Think in small steps: a Dashboard_Main object is the 'owner' of that connection; a DashBoard_Gui object owns a Dashboard_Main object so a Dashboard_Gui object can use that connection object, i.e. it just has to 'ask' for it from that Dashboard_Main object and since that connection is a public object all other things owning a Dashboard_Main object can simply 'take' the connection by referring to it, e.g. dm.connection.
If you're interested in some theoretical aspects of all this, google for "Law of Demeter".
kind regards,
Jos
- 10-22-2009, 06:45 PM #5
Member
- Join Date
- Dec 2008
- Posts
- 50
- Rep Power
- 0
Similar Threads
-
What are Instance variables and static variables?
By sandeshforu in forum New To JavaReplies: 3Last Post: 09-09-2009, 06:48 PM -
Public static method error
By leapinlizard in forum New To JavaReplies: 5Last Post: 04-30-2009, 12:10 AM -
accessing variables
By emp in forum New To JavaReplies: 3Last Post: 04-23-2009, 05:36 AM -
accessing a one class's non static variable from another class
By aruna1 in forum New To JavaReplies: 6Last Post: 03-31-2009, 05:27 AM -
accessing instance variables from static methods
By ravian in forum New To JavaReplies: 7Last Post: 03-01-2009, 11:09 PM
Bookmarks