Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Regarding static variables as global variable in Java....

  1. #1
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Regarding static variables as global variable in Java....

    I kind of want a variable to be global, except you can't do that in Java. I tried making it static instead, but it's still not working when I do that....

    Here's the situation:

    I am making a TicTacToe game that operates by creating a server and receiving requests from clients to play the game.

    All player information (number of wins, number of losses, name, symbol, etc) is written to a file, for every player who plays the game.

    The first thing I do when a client connects is ask him if he's a new or existing player.

    Now, imagine this situation:

    Two clients connect, and the server establishes a game for these two players. One of the client tells the server that he is an existing player. However, it turns out that there is someone else in a different Tic Tac Toe game who is currently playing as that player.

    Obviously, then, I can't let this other guy connect and also be playing simultaneously under the same name.

    The problem is this: how do I check that? The key here is that this is happening across games, and not among two players in the same game.


    What I did was create an array of Strings called "playersInUse". Everytime someone starts a game, I add their name to this array. When they finish, I remove their name from the array.

    The problem, however, is that this array is a data member of what? ....the Server class. Even though I only create one server socket, I create many instances of the Server class, since each instance is itself a game. Thus, each game will have its own copy of "playersInUse", and it won't work across games.

    My solution was to make the data member static, but it still doesn't keep all the information - the array gets reset for each instance of the game.

    Why is this happening??





    And on a totally unrelated note, how do you delete a topic on this site? I tried to do it before when my question became moot, but all I could do was edit it....


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,722
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Regarding static variables as global variable in Java....

    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: Regarding static variables as global variable in Java....

    What's exactly the point of that? Please don't post in my topic if you aren't going to try to supply an answer.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,722
    Thanks
    45
    Thanked 1,974 Times in 1,948 Posts

    Default Re: Regarding static variables as global variable in Java....

    I'm letting everyone else here know about the other site where this is posted so they can check it before wasting time posting something here that is already posted on other sites.

    See: The problems with cross-posting
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    ineedahero (December 21st, 2013)

  6. #5
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: Regarding static variables as global variable in Java....

    Well I would have deleted this topic if I knew how. The formatting of the code is so annoying

    --- Update ---

    Anyways, since you're a mod, couldn't you just delete this topic? I doubt anyone would ever be able to read though that code.

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,472
    My Mood
    Mellow
    Thanks
    87
    Thanked 483 Times in 474 Posts

    Default Re: Regarding static variables as global variable in Java....

    There's something wrong with your design. There should be one server for multiple clients. The server should keep track of all clients, their IDs, game states, etc. This raw game data should not be "global" or available to any of the other clients except for those details the server wants to share when and how the server want to share them. Those details should be worked out in the client's interface spec.

  8. The Following User Says Thank You to GregBrannon For This Useful Post:

    ineedahero (December 21st, 2013)

  9. #7
    Member
    Join Date
    Sep 2013
    Posts
    64
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Default Re: Regarding static variables as global variable in Java....

    Yeah, I tried doing that before and I hit a brick wall.

    I made the Server its own class, and I made the Game its own class, and kept them separate.

    But then I had the Game as a class within the Server, and the Player as a class within the Game. This for some reason would not let me declare any Game instances from the Server class (it kept saying I can't reference a 'new' static variable from a non-static context....when I looked it up I saw that the problem was having 'nested' classes, so I just abandoned that entire way of organizing the code, even though I see why it makes more sense). I could not get around that.

    Having it this way should be able to work...plus it's the format of the example in my textbook (though the format doesn't include any file I/O). I just don't understand why the static variable isn't doing what I want it to do.

  10. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    3,472
    My Mood
    Mellow
    Thanks
    87
    Thanked 483 Times in 474 Posts

    Default Re: Regarding static variables as global variable in Java....

    I made the Server it's own class, and I made the Game it's own class, and kept them separate.

    But then I had the Game as a class within the Server, and the Player as a class within the Game.
    So separate them. Why is the Game class "within the Server?" Does that mean it's a nested class. like Server.Game? Same with Game.Player, so that it's Server.Game.Player? Why would you want it that way? Maybe I misunderstood what "class X within class Y" means.

    --- Update ---

    IMO, yes. Nested classes are typically for the sole use of the enclosing class, like listeners, GUI elements, etc., not for general use by other classes in the project. I imagine you'll have errors to overcome during the separation effort, but the overall success of your client/server project requires breaking them apart. Variables from the Game class required by the Player objects should be obtained through appropriate interfaces.

    Further, I suggest that Player objects shouldn't communicate with Game objects directly but rather through a server object which directs and controls game play. Client players would logon and request a game from the server, the server would match players and assign them to a game which it creates and initializes, and then the server (or a service/class it creates and directs) would provide the necessary game control and player interaction to play a game to its conclusion.

    A stepping stone to a better understanding of this separation of user interface, program data, and program control would be to build a GUI (or several) using an MVC design.

  11. The Following User Says Thank You to GregBrannon For This Useful Post:

    ineedahero (December 21st, 2013)

Similar Threads

  1. [SOLVED] How to make an array a global static variable
    By lil_misfitss in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 15th, 2013, 10:58 AM
  2. topic related to static final variable and static block !!!!
    By Arnab Kundu in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 18th, 2013, 12:12 PM
  3. global variables for threads
    By barhom in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 17th, 2013, 10:28 PM
  4. [SOLVED] Problem while getting a number from another thread
    By Koren3 in forum Threads
    Replies: 4
    Last Post: April 7th, 2009, 01:42 PM