-3

I have a Class A which has a static variable initialized to 0 and I am changing this value to 1 in other class. I am sharing code below

public class Server {
    public static int flag;

    public static void main(String[] args)
    {
        flag = 0;
        while(true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            if (flag == 1) {
                System.out.println("Yo man");
            }
        }
    }
}

public class Client {
    public static void main(String[] args) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Server.flag = 1;
    }
}
nbrooks
  • 18,126
  • 5
  • 54
  • 66
  • 4
    Both have different main methods, meaning you run them as two different programs meaning they run on different memory locations therefore not sharing the same allocations of the static variables – aviad Oct 23 '16 at 06:09
  • I think Java only call one main method. No matter if you have hundreds `main()` – user3502626 Oct 23 '16 at 06:09
  • aviad ,thanks for the help . But , how can i accomplish this functionality. – Arunim Chopra Oct 23 '16 at 06:13
  • 1
    Well, you can't do that by researching "client server communication in java", that's for sure. – Tom Oct 23 '16 at 06:15
  • @Arunim chopra, what is your goal? are you trying to build a real client server architecture? or just building a simple simulation program that registrates clients and operate also as a server? – aviad Oct 23 '16 at 06:34

2 Answers2

0

You have two classes with two main methods so they can not access each others static variables directly.
If you are looking to communicate between the client and server which have two distinct main methods, below is a pointer that can help.
How to have 2 JVMs talk to one another

Community
  • 1
  • 1
Adi
  • 134
  • 11
0

Just to accomplish your specific request, you can operate both client and server as threads:

public static void main(String[] args)
{
    new Thread(new Server()).start();
    new Thread(new Client()).start();
}

public static class Server implements Runnable
{
    public static int flag;

    @Override
    public void run()
    {
        flag = 0;
        while (true)
        {
            try
            {
                Thread.sleep(1000);
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }

            if (flag == 1)
            {
                System.out.println("Yo man");
            }
        }
    }
}

public static class Client implements Runnable
{
    @Override
    public void run()
    {
        try
        {
            Thread.sleep(1000);
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }

        Server.flag = 1;
    }
}

if you want to implement a real client server architecture i suggest you to do a full research on how to implement a client server architecture in java

aviad
  • 1,553
  • 12
  • 15