Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > J2EE
|
J2EE General J2EE (Java 2 Enterprise Edition) discussions. Questions not specific to EE will be redirected elsewhere.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the J2EE section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 22nd, 2004, 02:44 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to get a reference to the obect...

Well, it is probably quite simple, but I can't seem to make it work. The thing is that I would like to have a reference to the object from within the class that implements the object. I have got a class 'Users' in which I have got a method 'load'. I would like this method to initialize the object from a file, like this...
Code:
    public void load(String filename)
    {
        File f = new File(filename);
        if(f.exists())
        {
            ObjectInputStream ois = null;
            try
            {
                ois = new ObjectInputStream(new FileInputStream(filename));
                this = (Users)ois.readObject();
                ois.close();
            }
            catch(Exception e)
            {
                System.err.println("!-> error: " + e);
            }
        }
    }
    In the above code I have assigned the result from readObject to 'this', which would be the intuitive way to do this, however it does not seem to work.

I have tried to make a private instance of the class and then assigning 'this' to the new instance, but then I can't use the methods of the class, which I have extended, in this...
Code:
public class Users extends Hashtable implements Serializable
It seems to be an evil circle! A work-around would be to return an object of type 'Users' from the load method and then assigning it when calling load, but I do not like this solution, since I then have to make an instance of that class and then assign it to another!

Thanks!

Jacob.
__________________
Danish audio books for download at http://www.lytenbog.dk (Danske lydbøger til download).
 
Old February 23rd, 2004, 05:19 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

jacob,

You can't just use 'this' on it's own as 'this' refers to the current object reference. You need to declare a reference of the correct type within your class and then assign the object reference to it.

FileInputStream fis = new FileInputStream("temp.tmp");
ObjectInputStream ois = new ObjectInputStream(fis);
Date date = (Date) ois.readObject();
ois.close();

Alternatively, you could load the individual fields by using the various read methods provided by the OBjectInputStream class.


Cheers

Martyn
 
Old February 23rd, 2004, 05:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jacob,

Having just replied I realised that I should have added the following example.

//Add this to your main
Users newUsers = (Users)load("userList");

//Replace your load method with this
public static Object load(String file)
{
   try {
      ObjectInputStream load = new ObjectInputStream(new FileInputStream(file));
      return load.readObject();
   }
   catch (Exception e) {
      System.out.println("Could not load " + file);
      return null;
   }
}


Cheers

Martyn
 
Old February 23rd, 2004, 06:21 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply, however I think the method that you describe corresponds to the thing I meant to describe in this...
Quote:
quote:A work-around would be to return an object of type 'Users' from the load method and then assigning it when calling load, but I do not like this solution, since I then have to make an instance of that class and then assign it to another!
That is, to return an object and then assigning it when calling the method. I would like to make an instance of an object and then load, something like this...
Code:
Users usrs = new Users();
usrs.load("users.usr");
usrs.put("martyn", new User());
I am probably being fanatic , but it should in some way be possible to do this...!?

Thanks again

Jacob.
 
Old February 23rd, 2004, 06:38 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 440
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Now I am suddenly not sure that we meant the same! So here is the important stuff of the 'Users' class...
Code:
public class Users extends Hashtable implements Serializable
{

    public Users()
    {

    }

    public Users load(String filename)
    {
        Users users = null;
        File f = new File(filename);
        if(f.exists())
        {
            ObjectInputStream ois = null;
            try
            {
                ois = new ObjectInputStream(new FileInputStream(filename));
                users = (Users)ois.readObject();                
                ois.close();
            }
            catch(Exception e)
            {
                System.err.println("!-> error: " + e);
            }
        }
        return users;
    }
}
...and then I have some other (very simplified) class, namely the calling class...
Code:
public class MyExampleLoadUserClassForWroxP2PForum
{
    public static void main(String[] args)
    {
        Users users = null;

        try
        {
            users = new Users();

            // Possible!
            users = users.load("users.usr");

            // Prefer something like this!
//            users.load("users.usr");

        }
        catch(Exception e)
        {
            System.out.println("!-> error: " + e);        
        }
    }
}
This works, however is not what I would like! (I am aware of the wrong error handling).

Thanks!

Jacob.





Similar Threads
Thread Thread Starter Forum Replies Last Post
By value, By reference watashi C++ Programming 1 October 10th, 2007 11:04 PM
reference. scandalous ASP.NET 2.0 Basics 1 April 9th, 2007 07:29 PM
get reference name demac43 Excel VBA 2 November 20th, 2006 02:35 PM
in need of reference! alialibidad SQL Language 1 June 4th, 2006 05:14 AM
in need of reference! alialibidad ASP.NET 2.0 Professional 0 June 3rd, 2006 04:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.