• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

serialization + no valid constructor

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,



Could anybody explain me why I get runtime error:
java.io.InvalidClassException: Outer$B; no valid constructor
during deserialization?
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very interesting question!

OK. I tried your code as you provided it and with a couple of variations:

1) I've got the same exception when trying the example as it was.
2) I changed inner class A to be static. Then it worked.
3) I changed inner class A to implement Serializable, and of course that worked too.


The conclusion I get from the fore mentioned 'experiments' is that instantiation of inner class A (remember the JVM needs to call the no-args constructor as that class does not implement Serializable) fails because the JVM needs an instance of the Outer class in order to be able to call the constructor. That is why it worked when I changed A to be a static inner class (no instance needed) or when I declared A serializable (no instantiation needed).

What do you think? Am I on the good track on this one? I expect your commnents.
[ June 08, 2007: Message edited by: Sergio Tridente ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was going to move this, but changed my mind. As Sergio wrote making the class A Serializable seems to fix the problem. There are also a few discussions on the net about the ickiness of serializing instances of inner classes.
[ June 08, 2007: Message edited by: Barry Gaunt ]
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an interesting discussion, and for those of you who want to focus strictly on the exam, Serialization and inner classes are two topics that don't intersect on the real exam.
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic