implementing compareTo() - doubt
posted 18 years ago
When compiled the above code i got the following error
X is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable
But i implemented the compareTo method
I don't know where I am wrong?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
When compiled the above code i got the following error
X is not abstract and does not override abstract method compareTo(java.lang.Object) in java.lang.Comparable
But i implemented the compareTo method
I don't know where I am wrong?
live
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi Raja,
Guess: you might have declared your own "class Object{ }" at any earlier point of time. and you might have forgotten that.
Its one reason i can derive.
Lets Rock
Guess: you might have declared your own "class Object{ }" at any earlier point of time. and you might have forgotten that.
Its one reason i can derive.
Lets Rock

Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
raja kanak
Ranch Hand
Posts: 135
posted 18 years ago
Srinivasan... you are great!!!
You have great great memory. I can't express my feeling in words here. Even I forget the class in my local hard drive, but you remembered it. that is really really really really great.
And thank you very much.
[ March 01, 2007: Message edited by: raja kanak ]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by Srinivasan thoyyeti:
Hi Raja,
Guess: you might have declared your own "class Object{ }" at any earlier point of time. and you might have forgotten that.
Its one reason i can derive.
Lets Rock![]()
Srinivasan... you are great!!! You have great great memory. I can't express my feeling in words here. Even I forget the class in my local hard drive, but you remembered it. that is really really really really great.
And thank you very much.
[ March 01, 2007: Message edited by: raja kanak ]
live
posted 18 years ago
I serialized an instance of the above class with i = -100;
When I deserialized the object the state was
i = 11
j = 10
k = 0
This shows that the static block run after the state has been restored by deserialization.
Comments anybody ?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I serialized an instance of the above class with i = -100;
When I deserialized the object the state was
i = 11
j = 10
k = 0
This shows that the static block run after the state has been restored by deserialization.
Comments anybody ?
Srinivasan thoyyeti
Ranch Hand
Posts: 558
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi Victor,
The Thing is Whenever class loads static block will execute.
Static block nothing to do with serialization.
Some Observations...which will clear the concept
When you serialize and de-serialize and using
case 1) SampleClass { static int count = 0;}
count +=100;
1.1 SampleClass sc = new SampleClass();
1.2 serialize(sc);
1.3 sc = (SampleClass)deserialize();
sc.count --> 100;
serialization has nothing to do with count value; Just ignore 1.1,1.2,1.3. Now see you incrementing class variable.
Case 2)class SimpleClass{
static int count = 0;
public static void main(String... a){
SimpleClass sc = new SimpleClass();
sc.count++;
System.out.println(sc.count);
SimpleClass sc1 = new SimpleClass();
System.out.println(sc1.count);
}
}
output:
1
1
Now you may be clear; Serializaion has nothing to do;
But the static variable get Initialize to default values when class loads only.
Once class loads static vars extent(life) is till the program ends.
Scope depends on reachability of the class. and access specifier of static var;

[ March 01, 2007: Message edited by: Srinivasan thoyyeti ]
The Thing is Whenever class loads static block will execute.
Static block nothing to do with serialization.
Some Observations...which will clear the concept
When you serialize and de-serialize and using
case 1) SampleClass { static int count = 0;}
count +=100;
1.1 SampleClass sc = new SampleClass();
1.2 serialize(sc);
1.3 sc = (SampleClass)deserialize();
sc.count --> 100;
serialization has nothing to do with count value; Just ignore 1.1,1.2,1.3. Now see you incrementing class variable.
Case 2)class SimpleClass{
static int count = 0;
public static void main(String... a){
SimpleClass sc = new SimpleClass();
sc.count++;
System.out.println(sc.count);
SimpleClass sc1 = new SimpleClass();
System.out.println(sc1.count);
}
}
output:
1
1
Now you may be clear; Serializaion has nothing to do;
But the static variable get Initialize to default values when class loads only.
Once class loads static vars extent(life) is till the program ends.
Scope depends on reachability of the class. and access specifier of static var;

[ March 01, 2007: Message edited by: Srinivasan thoyyeti ]
Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Do instance initialization blocks execute when object is deserialized?
is an instance initialization block and it seems to have executed from the output stated.
Would someone please clarify this?
Thanks in advance.
is an instance initialization block and it seems to have executed from the output stated.
I serialized an instance of the above class with i = -100;
When I deserialized the object the state was
i = 11
j = 10
k = 0
Would someone please clarify this?
Thanks in advance.
Srinivasan thoyyeti
Ranch Hand
Posts: 558
posted 18 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hi,
Your interpretation gone wrong.
When an class implements Serializable, then "when you de-serialize the Object Constructor will not execute" "if constructor doesn't execute, instace blocks also will not execute"
Instance bolcks will only execute just prior to the constructor.
import java.io.*;
class X implements Serializable
{ int i = 1;
static int j = 2;
{ j = 10; i = 11; }
transient int k = 10;
public String toString(){
return ("[ i ="+i+" , j = "+j+" k = "+k+" ]");
}
}
public class TestSerial5{
public static void main(String... a){
X y = new X();
try{
y.i = y.j = y.k = 9999;
System.out.println("Before Serial >"+y);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("output.txt"));
oos.writeObject(y);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("output.txt"));
X z = (X)ois.readObject();
System.out.println("After Serial >"+z);
}catch(Exception e) { }
}
}
Output:
Before Serial >[ i =9999 , j = 9999 k = 9999 ]
After Serial >[ i =9999 , j = 9999 k = 0 ]
3)k is clear because its transient k=0;
if k is String k = null;
2)j is static so need to touch this output.
1)i hear will be 9999 the value just prior to Serialize. As discussed above.
Hope you are clear by now

[ March 02, 2007: Message edited by: Srinivasan thoyyeti ]
Your interpretation gone wrong.
When an class implements Serializable, then "when you de-serialize the Object Constructor will not execute" "if constructor doesn't execute, instace blocks also will not execute"
Instance bolcks will only execute just prior to the constructor.
import java.io.*;
class X implements Serializable
{ int i = 1;
static int j = 2;
{ j = 10; i = 11; }
transient int k = 10;
public String toString(){
return ("[ i ="+i+" , j = "+j+" k = "+k+" ]");
}
}
public class TestSerial5{
public static void main(String... a){
X y = new X();
try{
y.i = y.j = y.k = 9999;
System.out.println("Before Serial >"+y);
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("output.txt"));
oos.writeObject(y);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("output.txt"));
X z = (X)ois.readObject();
System.out.println("After Serial >"+z);
}catch(Exception e) { }
}
}
Output:
Before Serial >[ i =9999 , j = 9999 k = 9999 ]
After Serial >[ i =9999 , j = 9999 k = 0 ]
3)k is clear because its transient k=0;
if k is String k = null;
2)j is static so need to touch this output.
1)i hear will be 9999 the value just prior to Serialize. As discussed above.
Hope you are clear by now

[ March 02, 2007: Message edited by: Srinivasan thoyyeti ]
Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
| Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |









