Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

implementing compareTo() - doubt

 
raja kanak
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Srinivasan thoyyeti
Ranch Hand
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
raja kanak
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 557
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.

I understand it clearly now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic