0

I want to access variables sTerminalId & sTransactionId in Class ServerThread

public class ServerThread extends Thread {

    /* Some Code Omitted Here */

    public void run() {
            String sDataFromPOS="";
            //Get Some Value in sDataFromPOS
            byte[] bSendToPOS = SAXHandler.ParseXMLToString(sDataFromPOS.toString().substring(2));
    }
}

public class SAXHandler extends DefaultHandler {

    static String sTerminalId = "";
    static String sTransactionId = "";

    public static byte[] ParseXMLToString(String sXMLData) throws Throwable {
        /* Do Some Operation on String sXMLData & extarct value of sTerminalId & sTransactionId*/
    }
}

What i tried is

SAXHandler.sTerminalId

Will get me value but only for one run if run it again it gives me previous value. is there any alternative ? i am dealing with sensitive data so dont want to mess my code

4
  • 2
    Create Object and use it Commented Aug 29, 2013 at 11:15
  • if you dont want the values of the variables to be overwritten, dont make them static! especially since you are using multi threading Commented Aug 29, 2013 at 11:17
  • 1
    if you want to keep ParseXMLToString method static, return object which has byte[], sTerminalId & sTransactionId from that method Commented Aug 29, 2013 at 11:21
  • @AbdullahShaikh : I Have Multiple variable to be return by that method showed here only two. and that byte[] return will get deleted auto. once i get answer to my question :) Commented Aug 29, 2013 at 11:27

2 Answers 2

5

This is why statics are bad. Make the variables instance variables and create a new object instance for each run (let's ignore encapsulation for simplicity):

public class ServerThread extends Thread {
    public void run() {
        String sDataFromPOS="";
        //Get Some Value in sDataFromPOS
        SAXHandler handler = new SAXHandler();
        byte[] bSendToPOS = handler.parseXMLToString(sDataFromPOS.toString().substring(2));
        handler.sTerminalId .... // access instance variables
    }
}

public class SAXHandler extends DefaultHandler {
    String sTerminalId = "";
    String sTransactionId = "";

    public byte[] parseXMLToString(String sXMLData) throws Throwable {
        /* Do Some Operation on String sXMLData & extarct value of sTerminalId & sTransactionId*/
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

So, is it ok if create object of class and call non static method inside Thread ? does it have any advantage over static method ?
Using statics means that all tread instances share the same variables, and I think that's not what you want. Generally, threads should share as few state as possible. Creating objects inside threads is very common, so don't worry.
0

Reinitialize your variable again before processing... like

public static byte[] ParseXMLToString(String sXMLData) throws Throwable {

     sTerminalId = "";
     sTransactionId = "";

    }

2 Comments

Is it a most legitimate way to do this ?
yes this is correct and work.... but it is better to use the approach suggested by noise in about post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.