Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5978205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:28:50+00:00 2026-05-22T21:28:50+00:00

I am writing a game which has a set of rules which should be

  • 0

I am writing a game which has a set of rules which should be loaded or saved from/to an XML file.

I have a UnitType class which can serialize and deserialize itself.
I also have a Faction class which can load/save to XML.
And I have a Rules class which contains

UnitType[] unitTypes;
Faction[] factions

When loading the rules, I first load all unit types objects and store their references into unitTypes Then I load Factions and store references to them into factions;

However, Faction class also has a member array UnitType[] accessibleUnitTypes, which shows which unit types this faction can build, and I need to load it, too.

Here comes the problem: since a reference is just an address, serializing it makes no sense (and also is impossible in Java), so I need to create a special field long ID inside the UnitType class and a special Manager class which has a method getUnitTypeByID(long ID). From now on, each time I create a UnitType object, I need to register it within the Manager (Manager has a HashMap<Long, UnitType> which maps IDs to the objects).

Now comes the problem that in java I can’t explicitly delete objects. I can unregister the object from the Manager inside the finalize() method but since the reference to the object is still in the Manager, finalize() will never be called.

This makes me write another method onDeleting() inside UnitType, which should be called like delete in C++ each time the object is no longer needed. The onDeleting() method would then tell Manager to delete the reference to the object.

In the example above I mentioned only one class needing onDeleting() method, but say, I have a whole heirarchy of classes, and all of them need to serialize and deserialize themselves to XML.

I end up with the necessity to add onDeleting() method to any such class, and make sure that inside this method any memeber variables run their onDeleting() and so on. This now looks much like C++ where you’d call delete on pointers. This is probably not the Java way to do this. Is there any alternative?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:28:50+00:00Added an answer on May 22, 2026 at 9:28 pm

    It actually depends on how you do the XML<->Java mapping. If you use JAXB, you can use @XmlID / @XmlIDRef. If you roll your own, look into IdentityHashMap, and just generate back-references.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a game which uses opengles. I have created my renderer class
I'm writing an iOS game (Using UIView), which has a randomly generated 2D city.
I'm writing an online game which allows a user to progress from one puzzle
I am writing a little game which has many circles moving on the screen.
I'm writing a physics-based game which has planets with gravity acting upon various objects
I have an object named game which has a property called channels (which is
I'm writing a bot for a game, which has a C++ API interface (ie.
I'm writing a game which uses a border layout with a JPanel using BorderLayout.CENTER.
I am writing a game which when given a partially filled word, searches a
I'm writing a game in which a thread - GameThread - loops forever, updating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.