I've been trying to develop a simple application using the new 6.5 Java
proxy generator. I successfully generated a Java proxy class for a PB
NVO. But to use this class in a Java app I am forced to declare all
variables as STATIC. The online help says-
Define global variables for JDPB_Connection and your generated object:
JDPB_Connection myConnection = null;
n_cst_gentest myGentest = null;
Global variables ?! AFAIK, you can't have any in Java, right? So I
declared them as members of my main class. But when I call methods in
the proxy, or even - 'new JDPB_Connection(...)' the compiler gives the
following error -
Can't make a static reference to nonstatic variable <varname> in class
myclient
So I am forced to declare all classes and variables related to
com.sybase.dpb.* and my proxies as static. What am I missing here? Also
is there any example app available that demostrates how to use the Java
proxies?
thanx
Jiggy
_________________________________________________________________________
eMail:jig...@usa.net
Web: http://www.geocities.com/SiliconValley/Horizon/3837/
Jiggy wrote:
> Hello-
>
> I've been trying to develop a simple application using the new 6.5 Java
> proxy generator. I successfully generated a Java proxy class for a PB
> NVO. But to use this class in a Java app I am forced to declare all
> variables as STATIC. The online help says-
>
> Define global variables for JDPB_Connection and your generated object:
> JDPB_Connection myConnection = null;
> n_cst_gentest myGentest = null;
>
> Global variables ?! AFAIK, you can't have any in Java, right? So I
> declared them as members of my main class. But when I call methods in
> the proxy, or even - 'new JDPB_Connection(...)' the compiler gives the
> following error -
>
> Can't make a static reference to nonstatic variable <varname> in class
> myclient
>
> So I am forced to declare all classes and variables related to
> com.sybase.dpb.* and my proxies as static. What am I missing here? Also
> is there any example app available that demostrates how to use the Java
> proxies?
>
You should not decalre any variables as static. You can declare variables as
class variables or in the method itself depending on how you want to access
them.
Here is a very simple Java client implementation .
HTH.
======= begin javaclnt.java ================
import java.lang.*;
import java.io.*;
import com.sybase.dpb.*;
import uo_test;
class javaclnt {
public static void main(String args[]) throws IOException {
JDPB_Connection myConn = null; // connection object
uo_test myObj = null; // remote object
//
// Set the connection attributes
//
String ServIP;
int ServPort;
ServIP = new String("localhost");
ServPort = 9999;
System.out.println("");
//
// Make the connection
//
System.out.println("Making Connection to DPB Server");
try {
myConn = new JDPB_Connection(ServIP, ServPort);
System.out.println("Connection successfull");
} catch (Exception e) {
System.out.println("---------------------");
System.out.print("Exception in JDBC_Connection : ");
System.out.println(e + " : " + e.getMessage());
e.printStackTrace();
return;
} catch (Throwable t) {
System.out.println("---------------------");
System.out.print("Throwable error in JDBC_Connection : ");
System.out.println(t + " : " + t.getMessage());
t.printStackTrace();
return;
}
//
// Create remote object
//
System.out.println("Creating remote object");
myObj = new uo_test(myConn);
System.out.println("Remote object created");
//
// Call method (of_string that takes string by value and returns
string)
//
String s1 = new String("Test string");
String r1;
System.out.println("Calling method uf_test");
try {
r1 = myObj.of_string(s1);
System.out.println("Method returned : " + r1);
} catch (RemoteException e) {
System.out.println("---------------------");
System.out.print("RemoteException on method : ");
System.out.println(e + " : " + e.getMessage());
e.printStackTrace();
}
//
// Disconnect
//
System.out.println("Disconnecting from DPB Server");
myConn.disconnect();
System.out.println("Disconnect successfull");
//
// Exit program
//
return;
}
}
======= end javaclnt.java ================
> thanx
> Jiggy
> _________________________________________________________________________
>
> eMail:jig...@usa.net
> Web: http://www.geocities.com/SiliconValley/Horizon/3837/
--
========================================================================
Ajit Sabnis *** Architecture for the New Enterprise ***
Sybase Inc. *** PowerBuilder 6.0 ***
========================================================================
javaclnt.java:20: Can't make a static reference to nonstatic variable
myConn in class javaclnt.
myConn = new JDPB_Connection(ServIP, ServPort);
^
Here's the modification
class javaclnt {
JDPB_Connection myConn = null;
// connection object
public static void main(String
args[]) throws IOException {
....
....
Is this because I am trying to assign a value to a non-static member inside a static method?
Jiggy
_________________________________________________________________________
eMail:jig...@usa.net
Web: http://www.geocities.com/SiliconValley/Horizon/3837/
Jiggy wrote:
Yes, the example that you gave does complie successfully. But if I move the connection declaration from a local variable to class member, I get the error I was talking about -javaclnt.java:20: Can't make a static reference to nonstatic variable myConn in class javaclnt.
myConn = new JDPB_Connection(ServIP, ServPort);
^
Here's the modificationclass javaclnt {
JDPB_Connection myConn = null; // connection object
public static void main(String args[]) throws IOException {
....
....Is this because I am trying to assign a value to a non-static member inside a static method?
Jiggy
Well if you want to make the connection object a class member, then you will have to create a new instance of the class (can't directly refer to class member without creating a instance). So you will have to declare a vraible of the class type and go from there. For example using the above definition :
class javaclnt {
JDPB_Connection myConn = null;
public static void main(String args[]) throws IOException
{
javaclnt myclass;
try {
myclass = new javaclnt();
myclass.myConn = new JDPB_Connection("localhost", 9999);
} catch ....
HTH
Jiggy
_________________________________________________________________________
Ajit Sabnis wrote:
>> > Ajit Sabnis *** Architecture for the New Enterprise ***
>> > Sybase Inc. *** PowerBuilder 6.0 ***
>> > ====================================================
>> > ===================
>> >
>>
>
>
> --
> ==
> =====================================================================