How to access a variable in another class?

Here are snippets of the two classes I currently have. I want to be able to access and retrieve the value of the SNMPResponse variable from the main class. If I try to call it SNMPGet.SNMPResonse from the main class it says that a "non-static variable SNMPResponse cannot be referenced from a static context" how would I make this work?

Class 1.

// Retrieves SNMP details of a device.

publicclass SNMPGet{

public String SNMPResponse;

// Retrieves the SNMP variables based on the string OID and stringAddress

public String getSNMP(String OID, String address)throws Exception{

String stringOID = OID;//"1.3.6.1.2.1.1.5.0";

String stringAddress = address;//"10.31.9.41/161";

// Target address

Address targetAddress =new UdpAddress (stringAddress);

OID targetOID =new OID(stringOID);

...

Class 2.

// Control the flow of the program.

publicclass OIDScript{

publicstaticvoid main( String [] args )throws Exception{

String output =null;

SNMPGet snmpget =new SNMPGet();

output = snmpget.getSNMP("1.3.6.1.2.1.1.5.0","10.31.9.41/161");

System.out.println(output);

}

}

Cheers,

Mike

Message was edited by:

carlo_bauer

[2173 byte] By [carlo_bauera] at [2007-11-27 4:18:51]
# 1
make it:public static String SNMPResponse;however you must know what "static" means otherway you won't be able to fully know what you're doing
calvino_inda at 2007-7-12 9:25:42 > top of Java-index,Java Essentials,Java Programming...
# 2
You have, in main(), a reference to an SNMPGet object, which is presumably you want the response from, so you use the reference to get the field as in:String response = snmpget.SNMPResponse;
malcolmmca at 2007-7-12 9:25:42 > top of Java-index,Java Essentials,Java Programming...
# 3

public class SNMPGet {

private String SNMPResponse; //PRIVATE!

public String getResponse() {

//compute the value of SNMPResponse, whatever

return SNMPResponse

}

...

}

public class OIDScript{

public static void main( String [] args ) throws Exception {

SNMPGet snmpget = new SNMPGet();

String output = snmpget.getSNMP("1.3.6.1.2.1.1.5.0", "10.31.9.41/161");

String response = snmpget.getResponse();

...

}

}

Hippolytea at 2007-7-12 9:25:42 > top of Java-index,Java Essentials,Java Programming...