vidhya
vidhya

Reputation: 2901

passing values across classes


I am doing a Java application in which i have two .java files (Connect.java and Handler.java).In Connect.java i have a function where i am getting some values from some other Response.java file . Now i need to pass these values as input parameters to a function in Handler.java . Following is my java file.Can any one help me please...?

Connect.java:

public class Connect
{

    private Response obj_response;
    public String UserId;
    public String LocationIpAddress;
    public String PassWord;

    public Connect(Response p_response)
    {
        obj_response=p_response;
    }

    public ConnectResponse handleConnect()
    {   

        try
        {   
            //values got from Response.java file
            String UserId=obj_response.getUserId();
            String LocationIpAddress=obj_response.getLocationIpAddress();
            String PassWord=obj_response.getPassWord();
        }
        catch(IOException e)
        {
            System.out.println("IO Exception");
        }

        return null;

    }
}

How can i implement my Handler.java?

Upvotes: 0

Views: 267

Answers (2)

Andreas Dolk
Andreas Dolk

Reputation: 114757

Handler, Connect, Response - it is pretty hard to tell, what those classes are supposed to model. My suggestion: try to find better names for those classes.

It looks like you have some sort of connection mechanism where you receive a response (from another service) and prepare it with some kind of (response-)handler for further processing (hope my crystal ball is not too dirty)

Without changing your actual design too much - the Connect class should own an instance of Handler (composition). The Handler class needs to offer one single method that takes a Response and does the handling:


public class Handler {
  public void handle(Response response) {
    // do what has to be done with response
  }
}

public class Connect {
  private Handler handler = new Handler();
  //...
  public ConnectResponse handleConnect() {   
       // ...
       handler.handle(obj_response);
       // create and return ConnectResponse
  }
}

Upvotes: 1

npinti
npinti

Reputation: 52185

You can either pass the parameters you need through the constructor of your Handler.java, or else, pass the data you need as function parameters. Did you try something out first?

Upvotes: 3

Related Questions