Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes implementing compareTo() - doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "implementing compareTo() - doubt" Watch "implementing compareTo() - doubt" New topic
Author

implementing compareTo() - doubt

raja kanak
Ranch Hand

Joined: Oct 18, 2006
Posts: 135

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
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
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


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
raja kanak
Ranch Hand

Joined: Oct 18, 2006
Posts: 135
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 ]
victor kamat
Ranch Hand

Joined: Jan 10, 2007
Posts: 247


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

Joined: Feb 15, 2007
Posts: 557
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 ]
Thulasi Prasanna
Greenhorn

Joined: Feb 21, 2007
Posts: 4
Do instance initialization blocks execute when object is deserialized?



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

Joined: Feb 15, 2007
Posts: 557
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 ]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thank you.

I understand it clearly now.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: implementing compareTo() - doubt