|
In order to avoid making every piece of code I write public, I am trying to figure out the correct way of accessing resources from other methods. I have the following style code;
import java.net.*;
import java.io.*;
public class Jeeves extends Thread {
private Socket socket = null;
public Jeeves(Socket socket) {
super("Jeeves");
this.socket = socket;
}
public void run() {
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
socket.getInputStream()));
String inputLine, outputLine;
while ((inputLine = in.readLine()) != null) {
ProcessLogin();
out.println("Hi.");
}
out.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void ProcessLogin() {
out.println("This worked");
}
|
|
Now in the method ProcessLogin, out.println("whatever") doesn't work because it belongs to run(), not ProcessLogin(). What is the correct way of getting to stuff that is in the scope of elsewhere?
|
|
|
You should pass the PrintWriter out to ProcessLogin.
semper fi...
|
|
|
|
|
|
|
|