Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Accessing java instance from another class?

Posted on 1999-07-20
4
Medium Priority
?
262 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?
0
Comment
Question by:eric07
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 120 total points
ID: 1246596
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
 
LVL 16

Expert Comment

by:imladris
ID: 1246597
Which method is more appropriate depends on your exact circumstances and requirements.

0
 
LVL 16

Expert Comment

by:imladris
ID: 1246598
Is there something else you would like clarified before you grade the answer?

0
 

Author Comment

by:eric07
ID: 1246599
Sorry took some time,  I finally was able to confirm and test it and it worked great.  Thanks for the info.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question