W
W
WannaCreative2016-11-03 22:02:35
Java
WannaCreative, 2016-11-03 22:02:35

Why doesn't [Getters and Setters] work?

there are 2 files.
String IP must change. but it doesn't happen, why?
THE CODE

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Client
{
    private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static String IP;

    public Client() throws IOException
    {
        IP = "192.168.1.5";
        index(IP);
    }

    public static void setIP(String IP)
    {
        Client.IP = IP;
    }

    private void index(String IP) throws IOException
    {
        System.out.print("What do you want to do? --> ");
        String command = reader.readLine();
        if (command.equals("Change my IP adress"))
        {
            System.out.println("Your IP before changing: "+getIP());
            new Soft(IP);
            System.out.println("Your IP after changing: "+getIP());
        }
    }

    public static String getIP()
    {
        return IP;
    }

    public static void main(String[] args) throws IOException
    {
        new Client();
    }
}

SECOND
import java.util.Random;

public class Soft
{
    private String IP;
    Random rand = new Random();

    public Soft(String IP)
    {
        this.IP = IP;
        changeMyIPAdress(IP);
    }

    private void changeMyIPAdress(String IP)
    {
        this.IP = rand.nextInt(200)+"."+rand.nextInt(200)+"."+rand.nextInt(200)+rand.nextInt(200);
        Client.setIP(IP);
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vorh, 2016-11-03
@WannaCreative

To catch such errors, use the debug mode:

private void changeMyIPAdress(String IP)
    {
        this.IP = rand.nextInt(200)+"."+rand.nextInt(200)+"."+rand.nextInt(200)+rand.nextInt(200);
        Client.setIP(IP); // Client.set(this.IP);
    }

M
mafia8, 2016-11-18
@mafia8

In the first file, the main function inside the class. Take outside.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question