accessing static method variable
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   poppy_123_poppy_123
Posted On:   Friday, December 19, 2003 10:05 AM

hello guys, how i can access the variables of static main function from other function e.g public static void main(String []a)throws RemoteException { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String s = "jdbc:odbc:addressbook"; Connection con = DriverManager.getConnection (s, "", ""); Registry reg=LocateRegistry.createRegistry(1099); Server server=new Server(); reg.rebind("rmi://127.0.0.2/server",server); } now how i can access con object in this function. void display() { Statement stmt=con.createStatement(); } thanks,    More>>

hello guys,
how i can access the variables of static main function from other function e.g

public static void main(String []a)throws RemoteException
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String s = "jdbc:odbc:addressbook";
Connection con = DriverManager.getConnection (s, "", "");
Registry reg=LocateRegistry.createRegistry(1099);
Server server=new Server();
reg.rebind("rmi://127.0.0.2/server",server);
}


now how i can access con object in this function.

void display()
{
Statement stmt=con.createStatement();
}

thanks,

   <<Less

Re: accessing static method variable

Posted By:   Michael_Couck  
Posted On:   Saturday, December 20, 2003 06:25 AM

The short answer is you can't.
The JVM reads the main(String[] args) signature as an entry point for it's own threads. Method variables (i.e. variables declared and allocated inside a method) are implicitly private so no access from outside the method.

You can do somthing like this though.

static String s;
static Connection con;
public static void main(String []a)throws RemoteException {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
s = "jdbc:odbc:addressbook";
con = DriverManager.getConnection (s, "", "");
Registry reg=LocateRegistry.createRegistry(1099);
Server server=new Server();
reg.rebind("rmi://127.0.0.2/server",server);
} //now how i can access con object in this function.
void display() {
Statement stmt=con.createStatement();
}


See ya
Michael
About | Sitemap | Contact