Results 1 to 10 of 10
  1. #1
    user1234 is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default marking a field Serializable

    Hi ,

    I would like to know the intended behaviour when you mark a field with in a class of Type Serializable

    eg private Serializable xyz

    Though the class migth not implement Serializable.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default

    You're not marking it as Serializable.
    You're saying attribute "xyz" is of a class that implements Serializable.

    Which is pretty useless in most cases.

  3. #3
    user1234 is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    so suppose for a declaration

    private Serializable parentId;

    what would a parentId be of type and what would the potential values of parentid be of?
    if parentId is of a class that implements Serializable then taht could be any class.

    can you tell me where exactly and in what context can it be used.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default

    You don't use Serializable like that, unless you're doing something funky which someone other that me would have to explain.

    You declare classes as Serializable, and define attributes as transient if you don't want them to be serialised.

    Your "parentId" example would simply inform whatever system you have that parentId is Serializable. That's it.

  5. #5
    user1234 is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    Exactly its the first time I have seen it -
    if thast what it tells the compiler then what use is it if teh class itself is not considered Serializable and what is the datatype of parentid in this case? that is confusing me

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default

    Does the class do anything with it?
    Not knowing what the situation is I really can't tell you what it's doing.

    Where did you see this code?

  7. #7
    user1234 is offline Member
    Join Date
    May 2010
    Posts
    4
    Rep Power
    0

    Default

    no ther are just attributes defined as mentioned above - and then there are just constructers and setters and getters thats it . so a class dow not implement serializable but has these fields

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default

    The only thing I can think of, without knowing anything else at all about what it is you're looking at (so this could well be wrong), is that the class is some sort of serliazing mechanism, which holds a bunch of Serializable things. Why? I can't say because I don't have the code.

    You do...

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You are more likely to see it in conjunction with inheritance for framework code. e.g A database key is most useful if it is Serializable (and Comparable) so you could have
    Java Code:
    import java.io.Serializable;
    public interface Entity<I extends Serializable & Comparable<I>> extends Serializable, Comparable<Entity<I>> {
    	I getId();
    }

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default

    Does that match the description given so far though?

    They're saying they have a class (unless I'm misreading this badly) that looks like (well, with getters and setters etc):

    Java Code:
    public class Something {
        Serializable someAttribute;
        Serializable anotherAttribute;
    }
    That doesn't seem to be to be quite the same beast.

Similar Threads

  1. Replies: 4
    Last Post: 01-17-2010, 11:13 PM
  2. Serializable problem in writeObject method
    By nouvaki in forum New To Java
    Replies: 3
    Last Post: 01-16-2010, 01:56 PM
  3. Casting to an List<Serializable>
    By Whatty in forum Advanced Java
    Replies: 5
    Last Post: 10-15-2009, 07:47 PM
  4. Forum Rules: Marking your Thread as Solved
    By DonCash in forum Forum Guides
    Replies: 0
    Last Post: 04-01-2008, 12:13 PM
  5. Implementing Serializable interface
    By javaplus in forum Advanced Java
    Replies: 4
    Last Post: 12-18-2007, 12:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •