How to access a private attribute of a class in another class?

Asked

Viewed 2,915 times

1

I created a chat in Java. In the class Servidor, the user passes as input the port on which the server will run. In order for the chat to work correctly, I need to access this class variable Cliente, passing it as a parameter so that the client can connect to the server.

But I’m not being able to access it, I try to create a new server, but it doesn’t work. It was the only way I thought to access, but I think it would be wrong, because the server is already created within the class itself.

 public class Servidor {
 private Scanner scan = new Scanner(System.in);
 private ServerSocket servidor;
 private int numero_porta;

 public static void main(String args[]) throws IOException {
    new Servidor().iniciarServidor();
 }
 public int pegarPorta(){
    System.out.print("Digite a porta: ");
    numero_porta = scan.nextInt();
    return numero_porta;
 }
 public int getNumero_porta() {
    return this.numero_porta;
 }
 public void iniciarServidor() throws IOException {
    servidor = new ServerSocket(numero_porta);
    System.out.println("Servidor rodando na porta " + pegarPorta());
 }

public class Cliente {
private int porta;
public static void main(String args[]) {
    Scanner scan = new Scanner(System.in);
    Socket socket;
    String nome;
    Servidor server = new Servidor();
    String endereco;
        System.out.print("Digite um endereco IP: ");
        endereco = scan.nextLine();
        socket = new Socket(endereco, server.getNumero_porta());
}
  • Add the relevant code to your question. I don’t know anything about Java, so I can’t help you, but I believe that if you add the relevant code and detail what you intend to do better, concrete answers will appear to help you solve your problem. Welcome :)

3 answers

0


If the user type the port on the server, there is no way to pass it directly to the client, because it will be executed in another instance of the program, that is, another process with another JVM that has a memory area completely separate from the other.

In any communication program (Whats App, Facebook Messenger, etc.), the server needs to have a fixed address so that client applications can connect to it.

My suggestion would be to fix the server port. This works unless you need to have multiple servers, which is very unlikely.

If you need something more advanced, there are techniques for a server to register somewhere and clients can check the log to see if there is one or more servers available. This can be done, for example, by creating a third program called Registro where a server program can automatically add itself to a list and client programs can refer to that list to connect to one of the registered servers.

In a local example, it would be possible for client and server to run within the same program and thus share a common variable. To do this simply put the initialization of the two classes within the same method main. So the correct way to pass a variable would be more or less like this:

public static void main(String args[]) throws IOException {
    System.out.print("Digite a porta: ");
    Scanner scan = new Scanner(System.in);
    int numeroPorta = scan.nextInt();
    new Servidor(numeroPorta).iniciarServidor();
    Socket clientSocket = new Socket("127.0.0.1", numeroPorta);
}

Note that I passed the port to the server and to the client socket.

The rule is to correctly divide the responsibilities, that is, a server or client class should not be responsible for recovering the port or other settings. This should be delegated to another class or method and the required values passed at the time of building the object or at the server startup.

0

I don’t know if I understand it very well, but would putting a public 'getPorta' method in your Server class not solve your problem?

Ex:

public class Servidor {

   private int porta;

   public int getPorta() {
      return this.porta;
   }

}
  • It didn’t work. In order for me to access this get, I would need to create a new server within the Client class. But as the Server is initialized within the class itself, I think I end up losing the port reference.

  • The error is on this line: socket = new Socket(address, server.getNumero_port());

0

Your question was not very clear without the code, but I believe that putting a final static variable in your Server class would solve your problem. Note: I am counting on the idea that Server and Client are in the same program/ machine.

Ex on the server:

public class Servidor {

   public static final int PORTA = 12345;       

}

So when accessing this variable you do not need to instantiate the server object. And on the customer:

socket = new Socket(endereco, Servidor.PORTA);
  • This will not work as the user must inform the port he wants to run the server. I use a scanner to pick up the user input and step as parameter to the port.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.