1

I am new to Java and encountered this problem: I am learning how to save object state to a file and I got stuck with passing an array to the constructor. I believe that the problem is the base class with the constructor but I am not sure.

here is my Hero class:

import java.io.Serializable;


public class Hero implements Serializable{

/**
 * 
 */
private static final long serialVersionUID = 1L;

private int power;
private String type;
private String[] wepons;


public int getPower() {
    return power;
}


public void setPower(int power) {
    this.power = power;
}


public String getType() {
    return type;
}


public void setType(String type) {
    this.type = type;
}


public String[] getWepons() {
    return wepons;
}


public void setWepons(String[] wepons) {
    this.wepons = wepons;
}

public Hero(int powerH, String typeH, String[] weponsH) {
    this.power = powerH;
    this.type = typeH;
    this.wepons = weponsH;
}

}

and here is class which I try to use to save the object state:

import java.io.*;
public class SaveGame {

public static void main(String[] args) {

    Hero hero1 = new Hero(50, "Elf", new String[] {"bow", "short sword", "powder"});


    try{
        ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("Game.ser"));
        os.writeObject(hero1);
        os.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    ObjectInputStream is;

    try {
        is = new ObjectInputStream(new FileInputStream("Game.ser"));
        Hero p1N = (Hero) is.readObject();
        System.out.println(p1N.getPower() + " " + p1N.getType() + " " + p1N.getWepons());
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

}

Can you tell me and explain what am I doing wrong. Do I really need setters and getters in my Hero class and I have the feeling that I am using them incorrectly.

My problem was that when I tried to print out the Hero's parameters I got content of the array instead of string representation of the array. Thanks to user2336315 I know now that i should use Arrays.toString method when printing content of an array

Lenny
  • 35
  • 2
  • 7
  • 2
    What actually happens? Do you get a compilation error? An exception? Does something behave differently from your expectations? – user2357112 Jan 07 '14 at 11:03
  • 3
    Has no relevance, but you spelled `wepons` wrong – Paul Samsotha Jan 07 '14 at 11:05
  • _"Do I really need setters and getters in my Hero class and I have the feeling that I am using them incorrectly."_ - `private` fields with getters and setters is good practice. And you seem to be using them fine. – Paul Samsotha Jan 07 '14 at 11:07
  • Your code seems to work fine – Serdar Jan 07 '14 at 11:10
  • Actually @peeskillet, using `private` fields with getters and setters is a good practice only when you need a mutable object or mutable fields. Let's take the OOP aside for a moment and consider the domain: a Hero can increase his `power` and have many `weapons` during his life, but he probably cannot change his `type` after birth. So, you should provide means to change the `type` only on object creation time, in this context. – Joao Piccinini Jan 07 '14 at 11:39

2 Answers2

5

I ran your code and everything seems to be fine. The only problem is that you want to print the content of the array itself, not the string representation of the array itself. So use Arrays.toString :

System.out.println(p1N.getPower() + " " + p1N.getType() + " " + Arrays.toString(p1N.getWepons()));

Output :

50 Elf [bow, short sword, powder]
user2336315
  • 15,697
  • 10
  • 46
  • 64
  • +1 good catch, that's probably why OP thinks theyre not using the getters correctly – Paul Samsotha Jan 07 '14 at 11:10
  • Thanks! That is my fault I didnt say what exactly was the problem. As you said when I tried to print this out it gave me content of the array instead of the String representation of array. Thanks again! – Lenny Jan 07 '14 at 11:40
  • Just remember to close the `FileInputStream` as you did with the `FileOutputStream`. You could use try-with-resource as pointed out on [this link](http://stackoverflow.com/questions/11622348/do-i-need-to-surround-fileinputstream-close-with-a-try-catch-finally-block-how) – Joao Piccinini Jan 07 '14 at 11:45
0

Deserialization mechanism creates the class using its meta data. It does not depend on the access levels of the members of the target class, inclusing constructors. (Your code will work even Hero class has private default constructor.)

Serdar
  • 383
  • 1
  • 9