Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

XMLEncoder - Stackoverflowerror - overriding equals

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm trying to write out a class to XML using XMLEncoder.

There's a few unique things about this class.

1) It has no default constructor
2) I'm overriding the equals method.
3) The class has a static int count which defaults to 0. Each object is assigned an ID based on the current count. The ID is used to compare two objects.

Since it has no default constructor, I'm using the defaultpersistencedelegate. Here's the code:



If you run this, you will get a stackoverflowerror (atleast under jdk1.6.0_01 on Windows). I'm not an XMLEncoder expert, I've narrowed the problem down and I came up with this simple Person.java example.

The combination of the static int ID and override equals seems to be the problem. If you change the equals to:



It will still give the same error. However, if you remove the equals method completely, it will work fine and the xml output is as I expected. But unfortunately, I need to override the equals method.

1) Does anyone know how I can fix this?
2) Why would "return super.equals(obj)" give different behavior than having no equals method at all?

Note: I realize if my class were really a Person class, the equals method should compare SSN, first and last name, etc. and not some made up int ID - this is just a simple example to demonstrate the problem.

Thank you!
 
Jonathan Janisch
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I solved it using my own persistence delegate to create a new Person. I don't understand why I had to do this, since I thought that was the purpose of the defaultpersistencedelegate. I did see some stuff in the source for defaultpersistencedelegate that involved checks for overriding equals - but I won't pretend like I understand how it works.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic