Two Laptop Bag*
The moose likes Java in General and the fly likes Threads,serialisation, static variables - java basic doubts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Threads,serialisation, static variables - java basic doubts" Watch "Threads,serialisation, static variables - java basic doubts" New topic
Author

Threads,serialisation, static variables - java basic doubts

Abhi Venu
Ranch Hand

Joined: Jul 09, 2009
Posts: 73
[color=blue]
I have following doubts

1.what are the disadvantages in usage of Runnable interfaces over extending a Thread class to create a thread?

2. Can static variables in a class be searialised?

3.If a class A has two variables AB,AC

A subclass of A ie B has two varaibles BC,BD and also it implements serializable interface

a method is called on a object of type B to searilize it ? will it work correctly or cause erorrs as its parent A doesn't implement serializable interface


A table, a chair, a bowl of fruit and a violin; what else does a man need to be happy?:Einstein
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Abhi Venu wrote:1.what are the disadvantages in usage of Runnable interfaces over extending a Thread class to create a thread?

Are there any? Perhaps the usage of an extra object is the only one - one Runnable plus one Thread instead of only one Thread. For the rest there are only advantages. See also Extending Thread Vs Implementing Runnable.

2. Can static variables in a class be searialised?

If they are serializable, but only if you serialize them yourself. They aren't serialized when you serialize an instance (after all, it's not an instance variable), but nothing prevents you from using writeObject with a static field.

3.If a class A has two variables AB,AC

A subclass of A ie B has two varaibles BC,BD and also it implements serializable interface

a method is called on a object of type B to searilize it ? will it work correctly or cause erorrs as i[/color]ts parent c

If class A is not serializable then AB and AC will not be serialized, and not be serialized. In this case the constructor without parameters will be called for class A. If there is none then an exception will be thrown.
Try it:
Try this, then remove the first constructor from class A and try again.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Abhi Venu
Ranch Hand

Joined: Jul 09, 2009
Posts: 73
Thanks alot for the prompt and beautiful descriptive reply
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
I have extended Thread when I wanted to use ThreadGroup to keep track of the state of a bunch of worker Threads.

Bill
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
1.what are the disadvantages in usage of Runnable interfaces over extending a Thread class to create a thread?

You only get to extend one class. If you extend Thread then you cannot extend something else that may help you.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

So that's an advantage of using Runnable, not a disadvantage.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Threads,serialisation, static variables - java basic doubts
 
Similar Threads
What is stored on Stack and Heap
confusion regarding serialization with static and transient variable
transient ????
Serializable interface.
Serialization