1

I am trying to create a system where one variable can be changed from different classes. The code that I am trying to create is this:

Server:

public class Server(){

   public int number = 0;

}

Client:

public class Client extends Server(){

   public void run (){
      number++;
   }
}

Business:

public class Business(){

  public void run(){
    number++;
  }

}

I have another class which instantiates the Client and Business class. The result when I print out the number from a different class is still 0, I am trying to understand the concept of how could the same variable be manipulated from different classes. Do I have to even extend classes, is there some other approach?

Sébastien Temprado
  • 1,413
  • 4
  • 18
  • 29
AndroidFreak
  • 866
  • 1
  • 10
  • 31
  • Do you call `run()` method? The default value of `number` is 0. That is probably what you see. And you'd better use getters\setters to manipulate an object's fields. [How do getters and setters work?](https://stackoverflow.com/questions/2036970/how-do-getters-and-setters-work) – Pavel Smirnov Mar 24 '19 at 19:40
  • 3
    I think you should start from the beginning. https://docs.oracle.com/javase/tutorial/java/javaOO/index.html – Nestor Sokil Mar 24 '19 at 19:46
  • @AndroidFreak you can create a static class where you can modify it variable. – DINA TAKLIT Mar 24 '19 at 19:49

3 Answers3

0

This is because in your case the number variable is an instance variable, which means that each instance has its own copy of this primitive. You will get more understanding by reading about instance and class variables. So if you will make your number variable as a class variable by adding static keyword - your application should start to work (provided that you fix compilation errors).

I also recommend you to read about AtomicInteger which would be more safety in multithreading.

P.S. After rewriting of your code I get following code:

class Test {
    public static void main(String... args) {
        Client c = new Client();
        c.run();
        Business s = new Business();
        s.run();
        Server ss = new Server();
        System.out.println(ss.number);
    }
}

class Server {
    public static int number = 0;
}

class Client extends Server {
    public void run() {
        number++;
    }
}

class Business extends Server {
    public void run() {
        number++;
    }
}
0

You have a simple methods to achieve this.

1) Use number as a Static variable and access the method. ( Use Synchronized method to increment the value for multi threaded application)

2) Use AtomicInteger to increment. ( No need Synchronization) (https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/atomic/AtomicInteger.html)

If you need more explanation please let me know.

0

You can use the solution above mentioned by @Mikita Berazouski. Or you can use Static Method. Like this you do not need to extend Server.

Static methods are the methods in Java that can be called without creating an object of class. They are referenced by the class name itself or reference to the Object of that class.

Your solution will be like this :

public class Test {
    public static void main(String... args) {
        Client c = new Client();
        c.run();
        Business s = new Business();
        s.run();
        System.out.println(Server.number);
    }
}

class Server {
    public static int number = 0;
    public static void run ()
    {
        number ++;
    }
}

class Client  {
    public void run() {
        Server.run();
    }
}

class Business {
    public void run() {
        Server.run();
    }
}

try the code above here and you will check the result.

Another possible solution since you declare number as static in class Server is to write code like this, this allow you to do diff thing in the void run of each class :

public class Test {
    public static void main(String... args) {
        Client c = new Client();
        c.run();
        Business s = new Business();
        s.run();
        System.out.println(Server.number);
    }
}

class Server {
    public static int number = 0;
}

class Client  {
    public void run() {
        Server.number ++;
    }
}

class Business {
    public void run() {
        Server.number ++;
    }
}
DINA TAKLIT
  • 7,074
  • 10
  • 69
  • 74