• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

Accessing java instance from another class?

Being new to java I'm trying to figure this part out.  Comming from a C++ background I would usually do this to access the instance of that class:

// File: input.h
class CInput{
    CInput();
    data();
};
extern CInput *input;

// File: test.cpp
void testing()
{
    input = new CInput();
}

// File: get.cpp
void get()
{
    input->data();
}

The problem i'm having w/ java is that if I create an instance on one class and I need to use a member fuction on another class it doesnt see it.  I'm using it this class for database connectoin
Ex.

public class DBConn{
   public Statement select = null;
   public ResultSet result =  null;

    open();
    close();
}

two java files:
// File: main.java
// in the member function: start()
public void start()
{
    DBConn dbconn = new DBConn();
    dbconn.open();
}
 
// but in a different java file I need to access a member function in the
// DBConn class
// File: test.java
public void intro()
{
    dbconn.close();
}

Is there a way to be able to see the instance of the DBConn object on a different class even thought it wasnt created there.

Any advice?
0
eric07
Asked:
eric07
  • 3
1 Solution
 
imladrisCommented:
In the C++ example you have created an external global variable named input, which is of type "pointer to CInput". In the Java case dbconn is a local ("automatic") variable that disappears at the end of the routine. The straight equivalent solution would be to make dbconn into a "global" variable. In Java this is accomplished by using the static keyword:

// File: main.java
// in the member function: start()

class MyApplet extends Applet
{ static DBConn dbconn;

public void start()
{   dbconn = new DBConn();
    dbconn.open();
}

You now access it with:
 
// but in a different java file I need to access a member function in the
// DBConn class
// File: test.java
public void intro()
{
    MyApplet.dbconn.close();
}

The MyApplet prefix tells the system where to find the variable dbconn.

Alternatively (and more OOPy) you could pass a reference to the MyApplet object that was created, and dbconn could be a member variable (instead of a static):

class MyApplet extends Applet
{ DBConn dbconn;

public void start()
{   dbconn = new DBConn();
    dbconn.open();
}

Then intro is passed a reference:

// File: test.java
public void intro(MyApplet ma)
{
    ma.dbconn.close();
}

The intro methods class might also have gotten the reference earlier and saved it in a member variable of its own.

0
 
imladrisCommented:
Which method is more appropriate depends on your exact circumstances and requirements.

0
 
imladrisCommented:
Is there something else you would like clarified before you grade the answer?

0
 
eric07Author Commented:
Sorry took some time,  I finally was able to confirm and test it and it worked great.  Thanks for the info.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now