Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. IT & Infrastructure
  4. Java - Sharing data across classes

Java - Sharing data across classes

Scheduled Pinned Locked Moved IT & Infrastructure
javagame-devsysadminhelptutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    ZippyV
    wrote on last edited by
    #1

    Hi, I'm creating a lobby application (for a small game) in Java but I have a problem: - The app opens a server socket and waits for clients to connect. - For every client that connects a new client-class is created which communicates with the client and receives info, like a username. My problem is how to share the info (like the username) with all the other client-classes? Every client needs to see a list of all connected users, one user should be able to send a message to another user and so on.

    A 1 Reply Last reply
    0
    • Z ZippyV

      Hi, I'm creating a lobby application (for a small game) in Java but I have a problem: - The app opens a server socket and waits for clients to connect. - For every client that connects a new client-class is created which communicates with the client and receives info, like a username. My problem is how to share the info (like the username) with all the other client-classes? Every client needs to see a list of all connected users, one user should be able to send a message to another user and so on.

      A Offline
      A Offline
      ATE_Engineer
      wrote on last edited by
      #2

      I think you can accomplish this with a class variable. A class variable is on that only has one instance and is shared between all copies of the class. It is a variable in the class that is declared outside the scope of all the methods of the class and has the static modifier in it's declaration. An example:

      public class ClientClass
      {
      public static Vector userNames = new Vector(0, 1);

      public ClientClass(String userName)
      {
      userNames.add(userName);
      }

      public Vector getUserNames()
      {
      return userNames;
      }

      }

      Use of above:

      ClientClass client1 = new ClientClass("user 1");

      ClientClass client2 = new ClientClass("user 2");

      Vector users1 = client1.getUserNames();
      Vector users2 = client2.getUserNames();

      In the above both users1 and users2 should be the same.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups