Serializing classes that contain other classes

Hi all,
Hoping someone can answer me a quick question, I'm familiar enough with Java + OO in general but just starting to try to put a multiplayer game framework together so running into a few areas I haven't encountered before... so sorry if this is a bit of a newbie sounding question...
I have a serializable class that contains a reference to another serializable class, very similar to:
public class ClassA implements Serializable
    private ClassB b;
    ...and some other fields...
    public ClassA ()
        super ();
    ...and a standard public getB and setB
public class ClassB implements Serializable
    int someValue;
    ...standard no arguments constructor...
    ...standard getSomeValue and setSomeValue...
}Now that all works fine, serializes fine, send it down an ObjectOutputStream down a network socket, deserializes the other end, all no problem. What I don't like is that to create a ClassA I have to do:
ClassA myClassA = new ClassA ();
myClassA.setB (new ClassB ());   <--- this line
myClassA.getB ().setSomeValue (5);because it could easily be ommitted (especially since the real situation is more complex that two simple classes like above) and I'll get a NullPointerException. Normally (in a non-serialized ClassA) I'd have put b = new ClassB (); in the constructor.
However I don't like the idea of doing that because when deserializing a ClassA, I imagine the deserialization mechanism creates its own ClassB object to set into ClassA? And so if in the ClassA constructor, I create my own ClassB, then the deserialization will then immediately go and create its own ClassB and the ClassB I created will go to the garbage collector, i.e. it was a pointless object to create?
Just wondering what suggestions anyone has on this, there must be a standard approach to deal with this?
Thanks very much,
Nigel.

Allthough you give a lot of information I do not quite understand what you mean, but maybe a little re-arrangement of the setB en getB methods will do it:
public ClassB getB() {
    if(b == null) {
        setB(new ClassB());
    return b;
public void setB(ClassB b) {
    this.b = b;
}

Similar Messages

  • Masking a movie clip that contains other movie clips

    hi,
    i am just trying out something in flash but it just doesnt
    work.
    I have a layer which has a movie clip which contains other
    movieclips in itself.
    Masking the layer that has the main movie clip causes the
    other movieclips in the main one to disappear when running the swf.
    Does any one have any idea how to get around this???
    thanks

    Hi,
    Recently, I have been putting together a flash presentation.
    And I am just wondering if the following might help you, in your
    communication with the said swf file:
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE,
    onComplete);
    function onComplete(event:Event):void
    event.target.content.thinggy_mc.y -= 100;
    Not the best example, but this allows you to target a mc
    within an external swf file. I think if you look up this code, you
    will have an answer ;)
    Kind Regards,
    Boxing Boom

  • Cannot send emails that contain other asian languages

    i can receive emails that contian other languages, like chinese and korean. However, well i reply emails in those languages, people only receive smybols. I try to send it to my own email address as well, I also only receive symbols, like question marks. I'm using rogers but I don't think it has anything to do with the carriers because it was okay before. 

    Hello Hans
    The problem I have can be explained with what has happened to me just today!
    I was sitting in the back garden and wrote two emails. (Normally my EE Brightbox router will allow me to use wifi in the garden so I thought it safe to send them.)
    Wrong!
    Because I had no wifi signal for whatever reason, the emails were sent via my network (3) and not via the EE wifi network.
    3 then attempted to send them via smtp.orangehome.co.uk (specified in my mailbox setup on my phone since EE insists I use that outgoing server) and I assume that EE then refused to accept them since they did not originate from an EE user. The phone (Lumia 920) at that point froze with the 'Sycnhronising' message static on the screen. When I went to 'email setup' I got a message to tell me that there was a problem with my email setup. Whether the main problem or not, my email password had been lost and even when re-entered, I had to switch the phone off and on to stop the  'Sycnhronising' message and get access to email.
    This was slightly different from previous situations - normally I get the numeric error message mentioned earlier. Even now that I have deleted the two emails from the outbox I still cannot use email until I delete and recreate my email account.
    I have my own email server which I would prefer to use for outgoing mail as opposed to EE and the Lumia does not offer any facility to alter outgoing ports (as far as I an see).
    Sorry if the reply is a bit wordy but I do feel that this situation deserves wider exposure as had I known earlier I would have chosen a broadband supplier who did not dictate the setting for outgoing mail server.
    John H.D. Tuach

  • Trying to Clone an array object that contains other objects

    I have an employee object with the fields name (String), Salary (Double), and hireDay (Date). Since the date field contains many int fields (year, month etc..), using a straight clone will not work.
    What I want to do is copy an array of employees into a new array.
    For example:
    Employee [] newEmps = (Employee[])OldEmps.clone()
    In the employee class, I implement the Cloneable interface and my code so far will copy one Employee. How do I change this to copy an array of empoyees? Specifically, since hireDay contains many fields, a straight super.clone of the array won't work. Here is my clone code:
         public Object clone()
         try {
              // call Object.clone()
         Employee cloned = (Employee)super.clone();
              // clone mutable fields
    cloned.hireDay = (Date)hireDay.clone();
         return cloned;
         catch (CloneNotSupportedException e)
         { return null; }
    Any help is appreciated. Thanks!
    -Eric

    There's so much I don't understand about that, it just
    isn't worth asking. Let me spell out my previous post
    since it seems to have gone right over your
    head.Employee[] emps;
    // here you fill in the array of Employees
    Employee[] clonedEmps = new Employee[emps.length];
    for (int z=0; z<emps.length; z++) {I understand this next line of code. My question lies within the clone() function
    clonedEmps[z] = emps[z].clone();OK - Let me try to clear things up. If we take from your example the original array "emps". Now for me, emps, is already filled up in main().
    So in main() when I call emps.clone(), my clone function starts running:
    // the employee class (which I created) implements the Cloneable
    // interface
    // This clone function is inside my employee class
    public Object [] clone()
    try {
    Employee [] cloned = (Employee)super.clone();
    // IS THIS NEXT LINE OF CODE REQUIRED TO MAKE A DEEP COPY? IF SO, How would I implement perhaps a for loop that would copy the hireDay from each element in the original array to the elements in the the new array?
    // Obviously, each employee (element in the array) has a hireDay
    // Please modify this next line of code to show me how.
    cloned.hireDay = (Date)hireDay.clone();
    return cloned;
    catch (CloneNotSupportedException e)
    { return null; }
    If I wanted to access say the "hireDay" field of the 3rd element in the original employee array INSIDE THE CLONE FUNCTION - then how would i access it?
    Thanks
    -Eric

  • ANYBODY GIVE ME THE TABLE/TABLES THAT CONTAIN TCODE AND DEVELOPMENT CLASS

    ANYBODY GIVE ME THE TABLE/TABLES THAT CONTAIN TCODE AND DEVELOPMENT CLASS

    Hi phani,
                 The table TSTC contains the transaction codes.
    Also the below links provides a lot of info on the Standard SAP Tables.
    http://www.sapdev.co.uk/tables/tables.htm
    Pls reward if useful..
    Thanks,
    Sirisha.

  • Why does my application give SerializationException although I have no serialized classes?

    I have created an application deployment tool for BizTalk, and it references and uses Microsoft.BizTalk.ExplorerOM apis to start, stop and manage a BizTalk application. Now when I run the tool, it gives following exception-
    Unhandled Exception: System.Runtime.Serialization.SerializationException: Type 'Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer' in Assembly 'Microsoft.BizTalk.ExplorerOM, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable.
    None of my classes are defined to be serializable. And the tool is created in WPF. Any idea how I might resolve this?

    Hi,
    I think the user has got this resolved at stackoverflow already.
    Why does my application give SerializationException although I have no serialized classes?
    Resolution: He was creating the instance of BtsCatalogExplorer through reflection, using connection
    string instead solves the issue.
    Rachit
    Please mark as answer or vote as helpful if my reply does

  • I never used LabView but need to generate a RS-233 serial output string that contains an IRIG timestamp based on a contact closure. Is LabView an appropriate tool to do this? How much does it cost to get started with the product?

    I never used LabView but need to generate a RS-233 serial output string that contains an IRIG timestamp based on a contact closure. Is LabView an appropriate tool to do this? How much does it cost to get started with the product?

    Labview isn't cheap, but it would certainly allow you to build a program (set of vi's - virtual instruments) that would do what you describe. Where would the timecode information come from? Your vi, or program, would: Start up, initialize the serial port, loop until a trigger happened, read the timecode (from where?), output that timecode out the serial port in a loop until you pressed the stop button. There is a free evaluation of Labview software available from NI - the base product itself runs about $995.
    - Dave

  • How do I assign a file to a drive other than the drive that contains my program software?

    I've just installed a video editing program on my new Mac Pro Xeon 64 bit workstation. I want to ensure that all my media files reside on a drive other than the drive that contains programs. I had no problem doing this on my old Dell. How do I do this on my Mac Pro? -- Gary

    Application Preferences.
    Next time, 'what' program would also help.
    Most people do setup with a dedicated boot drive with OS and apps, .then use other drives or array(s) for media, scratch as needed, including external hardware RAID.

  • I used a partitioned HDD for time machine, using a partition already containing other data files. I am now no longer able to view that partition in Finder. Disk Utility shows it in grey and "not mounted". Any suggestions of how to access the files?

    I used a partitioned HDD for time machine, using a partition already containing other data files. I am now no longer able to view that partition in Finder. Disk Utility shows it in grey and "not mounted". Any suggestions of how to access the files? Does using time machine mean that that partition is no longer able to be used as it used to be?
    HDD is a Toshiba 1TB, partitioned into two 500GB partitions.
    OS X version 10.9.2

    Yes, sharing a TM disk is a bad idea, and disks are cheap enough so that you don't need to.
    Now
    Have you tried to repair the disk yet

  • When I receive an email that contains a logo in a png format the logo is repeated twice.  Why is this happening in my email when it is not happening in other recipient's mail.

    When I receive an email that contains a logo in a .png format the logo is repeated twice.
    Why is this happening in my MAC maverick system when it does not happening in other
    MAC recipients' email?
    Nan

    WMP is a windows proprietary format.
    It is not supported on iOS devices.
    QuickTime player is built into ios and used for all video playback-nothing is being converted.
    You can try downloading an alternate player that can playback wmp or use a computer.

  • I recently got a new computer and lost the email that contained my serial number.

    I recently got a new computer, my old computer had all my adobe products installed on it and was backed up to an external hard drive. Consequently, I don't have my serial number that was sent to me via email, because it was lost when I lost my other computer. Is there a way I can get one resent to me?

    Your serial number should be available online if you purchased from Adobe or if you purchased somewhere else and registered your product with Adobe. 
    Check the My Orders or My Products and Services areas that you can see after logging in to www.adobe.com and hovering over your name at the upper right:

  • Why is it so hard to deploy EJBs that contain SQLJ???

    The SQLJ translator creates _SJProfilex.ser files by default that
    contain the SQL statements the SQLJ file uses. When trying to
    use a SQLJ file for an EJB, I can't get it to deploy right--in
    fact I'm thrawted at every turn! (THIS SHOULD BE EASY!)
    1) .ser files aren't deployed by default. O.K., I added the .ser
    rule in the deployment file. No dice, still doesn't show up in
    the JAR file!
    2) Even if I force the .ser file to be in the JAR (by including
    it in the project), still no dice! After deployment, the .ser
    file doesn't make it to the server.
    3) O.K., there's a nice -ser2class option to SQLJ that turns it
    into a class file instead of a .ser file. But where to add that
    option? It's not in the SQLJ options of the project properties.
    I also tried an SQLJ_OPTIONS environment variable, and putting an
    sqlj.properties file in all the suggested places. Nothing works!
    The only way I've been able to get it to work is to manually JAR
    the .ser file, ftp it to the server, and run loadjava on it.
    THERE HAS GOT TO BE AN EASIER WAY! ...so much for one-click
    deployment.
    Please help!
    null

    Hi John,
    There is an issue with deploying SQLJ files in 8.1.5.
    The doc (help sytem ) gives more details on how to deploy them.
    Open Online Help system
    Double click on the Sample Applications book icon
    Double click on the "web Application for Oracle8i" book
    Open "Deploying the EJB's"
    On the right hand window you will find a topic
    "Deploying the SQLJ files used by the Rentals EJB"
    this should take you through the deployment of SQLJ files.
    regards
    raghu
    John K. Peterson (guest) wrote:
    : The SQLJ translator creates _SJProfilex.ser files by default
    that
    : contain the SQL statements the SQLJ file uses. When trying to
    : use a SQLJ file for an EJB, I can't get it to deploy right--in
    : fact I'm thrawted at every turn! (THIS SHOULD BE EASY!)
    : 1) .ser files aren't deployed by default. O.K., I added the
    .ser
    : rule in the deployment file. No dice, still doesn't show up in
    : the JAR file!
    : 2) Even if I force the .ser file to be in the JAR (by including
    : it in the project), still no dice! After deployment, the .ser
    : file doesn't make it to the server.
    : 3) O.K., there's a nice -ser2class option to SQLJ that turns it
    : into a class file instead of a .ser file. But where to add
    that
    : option? It's not in the SQLJ options of the project
    properties.
    : I also tried an SQLJ_OPTIONS environment variable, and putting
    an
    : sqlj.properties file in all the suggested places. Nothing
    works!
    : The only way I've been able to get it to work is to manually
    JAR
    : the .ser file, ftp it to the server, and run loadjava on it.
    : THERE HAS GOT TO BE AN EASIER WAY! ...so much for one-click
    : deployment.
    : Please help!
    null

  • In coded ui how do you correctly retrieve a browser window that contains an embedded Adobe PDF reader in browser

    I'am running across this issue when I'm debugging or running my coded UI automation project, where i get the exception labeled "{"COM object that has been separated from its underlying RCW cannot be used." System.Exception {System.Runtime.InteropServices.InvalidComObjectException}"
    everytime i come from a browser window that contains a pdf reader embedded in it. This happens every time I retrieve the window and try to click back. It barfs when i perform the back method on it. I've tried different things but none has worked including
    the playback wait.
    var hereIsmypdf = ReturnPDFDoc();
    public BrowserWindow ReturnPDFDoc()
    Playback.Wait(1000);
    var myPdFdoc = GlobalVariables.Browser;
    return myPdFdoc;
    hereIsmypdf.Back();
    skinny_Will

    Hi skinny_Will,
    <<"{"COM object that has been separated from its underlying RCW cannot be used." System.Exception {System.Runtime.InteropServices.InvalidComObjectException
    According this exception error message, I did some research about it. I know that this exception error is occurred in
    COM object which was disposed if you are using the COM objects in a same static class or variables which ware shared between tests.
    So please you refer the following blog to use ExecutionThread element to instruct the test to use the MTA threading model. This way, the static instance of "store" can be shared across tests.
    http://blogs.msdn.com/b/martijnh/archive/2009/12/31/unit-testing-com-object-that-has-been-separated-from-its-underlying-rcw-cannot-be-used.aspx
    Note: if you are using the VS2013 to create the coded UI test, you will need to add a test setting file to add this
    ExecutionThread.
    https://msdn.microsoft.com/en-us/library/ee256991.aspx?f=255&MSPPError=-2147217396
    In addition, ad you said that when you playback the browser window that contains an embedded Adobe PDF reader and then it could not work.
    I suggest you could try to add more searchproperties to find this target control and then check this issue.
    I find a similar thread about your issue, please refer the Steve's code to check your issue.
    https://social.msdn.microsoft.com/Forums/vstudio/en-US/73445915-ac08-47de-8bab-b6b07294d0c2/coded-ui-test-wont-find-an-pdf-browser-window-it-times-out?forum=vsunittest
    Best Regards,
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • Servlet that contains native metrhods

              Hi,
              I'm getting an error of unstatisfied link error whenever I call a servlet that
              containes a native methods.
              Does anyone know how to do this? The Weblogic Frequently Asked Questions:(S-side
              java servlet) page
              it has the following at the end :-
              "if you choose to use WebLogic Server to serve your servlets, note that servlets
              using native methods that are not part of the WebLogic
              software will fail with an "Unsatisfied Link" error if they are placed in the
              servlet CLASSPATH. Be sure not to put classes containing
              native methods in the servlet CLASSPATH. Instead, put these servlets in the
              regular CLASSPATH of the WebLogic Server.
              For more information, see Setting up WebLogic Server as an HTTP server, or the
              Java WebServer FAQ at JavaSoft.
              

    Hi,
              A Unsatisfied link basically informs that a shared library could not be
              found in
              1)System path
              2) or the current path.
              Make sure that the shared library is in either of the above
              --Naggi
              "Amin Fakira" <[email protected]> wrote in message
              news:3ac3b70d$[email protected]..
              >
              > Hi,
              >
              > I'm getting an error of unstatisfied link error whenever I call a servlet
              that
              > containes a native methods.
              > Does anyone know how to do this? The Weblogic Frequently Asked
              Questions:(S-side
              > java servlet) page
              > it has the following at the end :-
              > "if you choose to use WebLogic Server to serve your servlets, note that
              servlets
              > using native methods that are not part of the WebLogic
              > software will fail with an "Unsatisfied Link" error if they are placed
              in the
              > servlet CLASSPATH. Be sure not to put classes containing
              > native methods in the servlet CLASSPATH. Instead, put these servlets in
              the
              > regular CLASSPATH of the WebLogic Server.
              > For more information, see Setting up WebLogic Server as an HTTP server,
              or the
              > Java WebServer FAQ at JavaSoft.
              

  • I know the name of an object - how do I get the name of the object that contains it?

    If I have a Movieclip, I can trace the name of the Movieclip
    that contains it using the _parent property.
    With a generic object, is there a way to trace the name of
    the object that contains it?
    Please, please tell me - this will be a huge help.
    Thanks

    Thanks for these comments - my problem isn't with MovieClips.
    I need to know if there is something which is the generic Objects
    equivalent to the MovieClips _parent property.
    Here's some code that hopefully will help explain further:
    class MyClass extends Object {
    var myObject:Object;
    function MyClass (){
    myFirstObject = new Object();
    myFirstObject.theParent = this;
    private function getNameOfParent(p_object:Object){
    // Some code to return the name of the object that contains
    p_object
    trace(p_object.__proto__); // Output: [object Object]
    trace(p_object._parent); // Output: undefined
    trace(p_object.theParent); // Output: [object Object]
    return ???
    public function doSomething(p_num:Number){
    var parentName = getNameOfParent(myObject);
    // do some more action...
    Timeline code:
    var myClass1 = new MyClass();
    myClass1.doSomething();

Maybe you are looking for

  • When generating WebHelp, "Adobe RoboHelp 8 has encountered a problem and needs to close" appears.

    I'm using RH 8.0.2.208 (both patches applied). This project is a child project in a merged project structure and it is the only one that I am having problems publishing. I've tried deleting the CPD file and creating a new layout, but the error still

  • Bridge craching in CS4

    Bridge keeps coming up with an error and then windows closes it down. have restarted and gone back into Bridge for it to do the same thing after a minute or so. This has happened 4 times this am. I am trying to edit a WEDDING!! Brides wait for NO ONE

  • Indesign CS4 Crashing with one doc only

    My Indesign CS4 crashes with only one document (my most valuable document, of course). It does not crash with other small docs. The exact problem occurs on two Intel Macs (Mac Pro 6gb of Ram and MacBook Pro with 2gb of Ram): The doc opens for about 5

  • Monthly Backup Plan using Incremental Backups

    I would like to have a monthly backup plan as explained in the oracle documentation: First Friday of the month: Full Backup (Incremental Level 0) Monday, Tuesday, Wednesday, Thursday: Incremental Level 1 2nd, 3rd, 4th Friday: Cumulative Incremental B

  • ORA-01422: -LISTAGG function

    HI, I'm using listagg function in my query which returns only one row if I run the query butwhich i create a procedure with same query and select into statement,it showing error-exact fetch returns more than.. error... Please help me in resolving thi