Reputation: 85
I have a Java class inside which I have defined 3 methods.
public class Test {
String session_id = null;
public String login()
{
//returns the session id
return session_id;
}
public void read()
{
//use the session id returned from login() method
}
public void logout()
{
//use session id returned from login() method
}
}
I want to know how I can use the session id that is returned in the login()
method in the other two methods.
Upvotes: 0
Views: 120
Reputation: 73
Make a call to the method that returns the relevant value. note in this instance you will not need an object as the call is within the same class as what is called. hence the code within the method will look like so:
String whatever = login();
Upvotes: 0
Reputation: 1809
You can call login method in both of these methods.
public class Test {
String session_id = null;
public String login()
{
//returns the session id
return session_id;
}
public void read()
{
String session_id = login();
}
public void logout()
{
String session_id = login();
}
}
Upvotes: 3
Reputation: 1435
You can try this instead..
private String session_id; //make getter and setter
public String getSession_id() {
return session_id;
}
public void setSession_id(String session_id) {
this.session_id = session_id;
}
public void login() {
setSession_id(session_id); //session_id is what you were returning in your code
}
public void read() {
getSession_id(); //and store it in something
}
public void logout() {
getSession_id(); //and store it in something
}
Upvotes: 0
Reputation: 69505
So session_id
is a global variable so you can simply use it:
public void read()
{
String local_id = this.session_id
}
public void logout()
{
String local_id = this.session_id
}
Upvotes: 0