import java.io.*;
public class Solution implements Serializable {
public static class A {
String nameA = "A";
//static String nameA = "A";
public A(){
}
public A(String nameA) {
this.nameA += nameA;
}
}
public class B extends A implements Serializable {
private String nameB;
public B(String nameA, String nameB) {
super(nameA);
this.nameA += nameA;
this.nameB = nameB;
}
// private void writeObject(ObjectOutputStream oos) throws IOException {
// oos.defaultWriteObject();
// oos.writeObject(this.nameA);
// oos.writeObject(this.nameB);
// }
// private void readObject(ObjectInputStream ois) throws IOException, ClassNotFoundException {
// ois.defaultReadObject();
// this.nameA = (String)ois.readObject();
// this.nameB = (String)ois.readObject();
// }
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(arrayOutputStream);
Solution solution = new Solution();
B b = solution.new B("B2", "C33");
System.out.println("nameA: " + b.nameA + ", nameB: " + b.nameB);
oos.writeObject(b);
ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(arrayOutputStream.toByteArray());
ObjectInputStream ois = new ObjectInputStream(arrayInputStream);
B b1 = (B)ois.readObject();
System.out.println("nameA: " + b1.nameA + ", nameB: " + b1.nameB);
}
}
would you be more kind to help with above code .
when i run this code: output is:
nameA: AB2B2, nameB: C33
nameA: A, nameB: C33
and when i make change in above code like
// String nameA = “A”; –commented this
static String nameA=”A”; — uncommented this.
output is:
nameA: AB2B2, nameB: C33
nameA: AB2B2, nameB: C33
i am not able to understand why output differs. I must be missing some vital concept here. . Already invested 4-5 hours but still did not understand. Hope, you would help with this.
thanks you so much. have a good day.
not able to explore/understand why program is giving different output.
Resolved
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
13 July 2020, 14:08
the 'static' keyword makes the variable a class variable and not an instance variable. That means that when it is static it is the same exact for every single object. Take for example:
When you run this code you will see that all the objects output their internal non static String to what was set for each; however the static String for all three are exactly the same, which would be the last value set for it - "Non-Static string test 3". As I said above the static variable belongs to the class it is declared and not to each individual object. You can actually access the static variable without an object like this:
0
Guadalupe Gagnon
13 July 2020, 14:08solution
+2
Guadalupe Gagnon
13 July 2020, 14:11
So in your code by making the string static it effectively just made it object independent, and that wouldn't pass the task because it wants you to take 2 different objects and restore the internal variables.
+1
Manish Sinha
14 July 2020, 07:38
Thanks again Gagnon. i think i understood why output is different now. Thanks.
0