1

I want to send the same object for two clients. I use object write unshared method and read unshared. The class of my object is:

package servidor;

import java.io.Serializable;

public class Token implements Serializable {
    private static final long serialVersionUID = -5399605122490343339L;

    public static String sender, receiver, content;
    String id;

    public Token(String sender, String receiver, String content){
        Token.sender = sender;
        Token.receiver = receiver;
        Token.content = content;    

    }

    public static Token returnToken(){
        Token token = new Token(Token.sender, Token.receiver, Token.content);
        return token;
    }

    public static String returnSender(){
        return Token.sender;
    }

    public boolean hasToken(String id){
        if(Token.sender.equals(id)){
            return true;
        }
        else {return false;}
    }
}

But, when I use the method returnSender on the client side, the return is null. I'm checked and I no have errors in receive the object by readUnshared(). I have in both sides the class Token include in server package.

I have one thread per client, and the threads seek the same object to send.

How can send the same object for various clients in different threads?

user207421
  • 305,947
  • 44
  • 307
  • 483

1 Answers1

2

When you serialize an object, your only serialize the fields of that object, not static fields. You could serialize static fields by adding writeObject and readObject but this is generally a very bad idea.

I suggest you avoid using mutable static fields in general for multiple reasons, this being just one of them. In this case it would fix your problem.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • But, if my arguments is not `static` type, I don't acces the same in other thread. I have a main class and another two classes responsible for send the object for the clients. In the main class, the logic of my problem is executed and the object is created. In the another two class, the object is send. – user3294746 Jun 05 '15 at 19:55
  • @user3294746 I think you misunderstand what `static` does. As http://stackoverflow.com/questions/797964/what-is-the-exact-meaning-of-static-fields-in-java explains, `static` fields are used to share a field between all instances of the *same* class. Your `Token` object will always have access to its fields, whether static or not, when referenced from any other class. – pens-fan-69 Jun 07 '15 at 13:24
  • @pens-fan-69, I'm use static field because I not create a instance of the class Token in my another class. My class one for example create my Token instance and my classe two access the method `returnToken()` and send to client via socket. The problem is, when the client use the method `returnSender()`, the method return `null`. – user3294746 Jun 08 '15 at 00:36
  • 1
    My problem is `Token token = (Token) entrada.readUnshared();`. The IDE says : "the variable token is not used" – user3294746 Jun 08 '15 at 00:50
  • 1
    @user3294746 the problem you have is you shouldn't be using static variables this way. You shouldn't be setting static fields in a constructor, this is almost always a sign of a bug. You can't serialize static fields. If you want to pass an object between instance of different classes, I suggest you pass the Token object as an instance to an instance of the class which needs it. – Peter Lawrey Jun 08 '15 at 06:06
  • @PeterLawrey, thanks for add more information. I understand now. – user3294746 Jun 09 '15 at 16:19