-1

I was working with a class extending a Swing Object Class (e.g. public myClass extends JFrame) and with methods/variables referring to Screen objects/DisplayMode/Graphics.

There were no issues. However when I tried to cast Grphics instance g to a Graphics2D object, I have gotten, seem-to-be-common error as explained in multiple places of SE with great answers and information.

I AM CURIOUS! Because I managed to solve the issue without adding a serialVersionID or without implementing serializable interface. And as said I don't need my class to be serialized. Since it was solved without having to implement/extend serializable interface or adding serialVersionID - I am just curious to understand in what scenarios compiler decides or treats a class as or better be serialized...I am not sure how else I can explain this.

What I want to know, how/under what criteria does Java Compiler qualify a class as a serializable and demand a Serial ID? In my case neither I required a serialization nor did I specify one.

Community
  • 1
  • 1
bonCodigo
  • 14,268
  • 1
  • 48
  • 91
  • The Java compiler has nothing to do with it whatsoever. This is a message from your IDE. You can configure it back to a warning I believe. – user207421 May 03 '14 at 23:37
  • possible duplicate of [What does it mean: The serializable class does not declare a static final serialVersionUID field?](http://stackoverflow.com/questions/2288937/what-does-it-mean-the-serializable-class-does-not-declare-a-static-final-serial) – user207421 May 03 '14 at 23:37
  • @EJP, please check that I have already seen those questions. I don't have a doubt or question about the WARNING! I want to know how Compiler is making the warning, on what basis. If I had configured my IDE (Eclipse) to be warning free - then I don't think I would have ever come across this. And if it's nothing to do with compiler....as you said...it's even more doubtful. – bonCodigo May 03 '14 at 23:43
  • And I have already told you that the compiler has nothing to do with it, as do all the answers to the duplicate. Surely you realize there is an 'instanceof' operator and an 'implements' keyword? – user207421 May 03 '14 at 23:45
  • @EJP I don't have implements in my class. I had to use `implements` to explain my question since the answerer Noob said my question was vague. You infact marked this as duplicate WITHOUT even reading/knowing the contents of my question - because I have quoted the question you showed in your comment as a reference point. – bonCodigo May 03 '14 at 23:52

2 Answers2

4
class SerializableClass implements Serializable

You just implement the interface. Java never demands a Serial ID; if you don't provide one it's determined at runtime.

You can also extend a serializable class:

class AnotherSerializableClass extends SerializableClass

Java classifies a class as Serializable if it either implements Serializable itself or extends a class which implements Serializable.

Since MyClass extends JFrame which implements Serializable, your class is serializable.

Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
  • I had no intention of implementing Serializable interface. I don't need it. I just want to know why in the world Java compiler suddenly shoots me with an SerialID saying there's this error :) I managed to fix the error without adding an ID. That's when I have this doubt now and curious to find out how it happened. I programme the class and I should decide if I need my class to be serialized or not. It isn't up to compiler, isn't it? – bonCodigo May 03 '14 at 23:15
  • Most IDE's show a warning if you don't have a serialVersionUID, but it's not an error. You are not required to have one. – Anubian Noob May 03 '14 at 23:16
  • Also your question is kinda vague, you might want to clarify it some more. – Anubian Noob May 03 '14 at 23:17
  • Let's forget about the SerialVersionID or having required one. So it's not about implementing the serializable interface... without implementing or extending or having added an ID I managed to solve the issue - that means my class never needed one and it's not serialized. Being as clear as I can be - how does Java defines a class to be serialized or class needing an ID? – bonCodigo May 03 '14 at 23:21
  • It is about implementing the Serializable interface. – Anubian Noob May 03 '14 at 23:22
  • 2
    You implement the Serializable interface when you need to store a copy of the object, send them it to another process on the same system or over the network. So maybe your class satisfy one of the 3 condition for it to be deemed as a Serialisabled class – Ashish May 03 '14 at 23:23
  • "What I want to know, how/under what criteria does Java Compiler qualify a class as a serializable and demand a Serial ID? In my case neither I required a serialization nor did I specify one." – Anubian Noob May 03 '14 at 23:25
  • 2
    Either it implements Serializable or extends a class that implements Serializable. – Anubian Noob May 03 '14 at 23:25
  • @Ashish thanks, now you have given me a some means to search and find out. I will see which condition is satisfied my class. You should post it as an answer. – bonCodigo May 03 '14 at 23:47
  • @AnubianNoob "extends a class which implements Serializable ." I searched online to verify if `JFrame` is implementing Serializable interface. **It does.** For this very line, it answers my question into a certain extent. But I am afraid your answer is the answer for some other questions. – bonCodigo May 04 '14 at 01:08
  • 1
    Now we have made your answer to *answer* my question and certainly it takes time to dig the roots. Thanks. – bonCodigo May 04 '14 at 01:42
  • @bonCodigo Sorry, I kinda beat around the bush there. Glad I could help though :) – Anubian Noob May 04 '14 at 01:43
1

There are two cases.

  1. The class isn't compiled and contains 'implements Serializable'.
  2. The class is compiled, and therefore loadable, and 'Serializable.class.isAssignableFrom(thisClass)' returns true, where 'thisClass' is the result of loading the class.

(3) Repeat for all the base classes.

Note that this is the IDE doing this, not the Java compiler. It's not a compile error.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • What do you think about user vkg's answer? It states "compiler complains..." I am digging further to understand the situation fully. I may be wasting my time rather than taking your answer which repeats "IDE is doing"... But I guess it's worth the reading, I need to read and get convinced about it. What I found out so far, [JFrame is *implicitly* `implements serializable` interface](http://docs.oracle.com/javase/7/docs/api/javax/swing/JFrame.html) – bonCodigo May 04 '14 at 01:33
  • I had already commented on that answer and he had already deleted it before you posted this comment, so why you're even asking is a bit of a mystery frankly. `JFrame` inherits from a class that implements `Serializable.` Specifically, `java.awt.Component.` That comes under (2) and (3) above. There's nothing further to dig into. – user207421 May 04 '14 at 07:26
  • What I want to dig in as per my my interest. Plus 8 hours ago, I had already *forced* modify an answer to fit into my question and accepted it. FYI I upvoted yours for IDE/Compiler/JVM talk. Further no one should mark questions as duplicates without verification. – bonCodigo May 04 '14 at 09:48