We help IT Professionals succeed at work.
Get Started

Accessing java instance from another class?

eric07
eric07 asked
on
292 Views
Last Modified: 2009-12-16
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?
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE