Results 1 to 5 of 5
  1. #1
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default 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 class
    Java Code:
    public class Dashboard_Main
    {
        public ResultSet rs;
        public DB_Connection connection;
    }
    I have three classes: Dashboard_Main, Dashboard_GUI and DB_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 GUI
    Java Code:
    public class Dashboard_GUI extends JFrame
    {
    	private void setTableInfo(Object cbContents)
    	{
    		rs = connection.createQuery("SELECT * FROM dashboard_data.families_products WHERE depot='"
    									+ strDepotSelected + "'");	
    	}
    }
    I get the error cannot find symbol: variable rs, same for connection

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    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

  3. #3
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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.
    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 05:33 PM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ribbs2521 View Post
    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.
    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

  5. #5
    ribbs2521 is offline Member
    Join Date
    Dec 2008
    Posts
    50
    Rep Power
    0

Similar Threads

  1. What are Instance variables and static variables?
    By sandeshforu in forum New To Java
    Replies: 3
    Last Post: 09-09-2009, 05:48 PM
  2. Public static method error
    By leapinlizard in forum New To Java
    Replies: 5
    Last Post: 04-29-2009, 11:10 PM
  3. accessing variables
    By emp in forum New To Java
    Replies: 3
    Last Post: 04-23-2009, 04:36 AM
  4. Replies: 6
    Last Post: 03-31-2009, 04:27 AM
  5. accessing instance variables from static methods
    By ravian in forum New To Java
    Replies: 7
    Last Post: 03-01-2009, 10: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
  •