Reputation: 354
I have to classes, and I need to input two values into one of the classes in my program.
import java.io.IOException;
import java.sql.*;
import static java.lang.System.out;
public class login{
private String username;
private String password;
public void logon() throws IOException {
System.out.println("Write your Name");
username = System.console().readLine();
System.out.println(username);
System.out.println("Write your Password");
password = System.console().readLine();
System.out.println(password);
try {
// Does stuff
}
catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
}
}
Mainly after username and password have been verified in the try catch. I need to transfer those two strings to another class. Seen Below.
public class ClientHandler {
protected Socket client;
protected PrintWriter out;
protected String Username;
protected String Password;
public ClientHandler(Socket client) {
//Does Stuff
}
}
I would like some pointers to how I can achieve this, either through constructors, or any other method that would be efficient.
Upvotes: 0
Views: 2388
Reputation: 1358
I need to transfer those two strings to another class.
You can do this by creating a setter method (or more than one).
You need to add these two methods to your ClientHandler class.
public void setPassword(String password) {
this.Password = password;
}
public void setUsername(String username) {
this.Username = username;
}
Then, you set username and password variables by using these setter method
ClientHandler handler = ... //Create this object somehow
handler.setUsername(username);
handler.setPassword(password);
In your case this should go in you 'try' block
Upvotes: 1