Sba
Sba

Reputation: 35

Creating an object from a saved one in its constructor

To be clear: I have an object which is continiously changing its values during the runtime and I want to save the created and modified object on a file. I found how to that and I found also how to read back the object saving it into one another. But the question is: is it possible to call the class constructor of my object with the only parameter of the file in which the object I want to retrieve is stored?

NeuralNetwork(File fs){
    ObjectInputStream ois;
    changeFileSave(fs);      //just sets the file as savefile for the future
    try{
        ois = new ObjectInputStream(new FileInputStream(_saveNet));  //_saveNet = fs
        this = (NeuralNetwork) ois.readObject();
    }
    catch(Exception e){
        //error message
    }
}

It gives me an error on 'this =' If it's possible how do I do something like this?

Thank you

Upvotes: 1

Views: 78

Answers (3)

fps
fps

Reputation: 34460

You can't assign this, since it is readonly. this always points to the instance itself and is used throughout the life of the object to access its methods and attributes.

If you want to create an instance of your NeuralNetwork class by reading data from a file, you could use a factory method:

public class NeuralNetwork {

    private NeuralNetwork() { // private constructor forces us to use the
    }                         // factory method to create instances

    public static NeuralNetwork loadFromFile(File fs) {
        ObjectInputStream ois;
        this.changeFileSave(fs); // just sets the file as savefile for the future
        try {
            ois = new ObjectInputStream(new FileInputStream(_saveNet));
            return (NeuralNetwork) ois.readObject();
        }
        catch(IOException e){
            throw UncheckedIOException(e);
        }
    }

    // other methods and attributes
}

Then, wherever you use your NeuralNetwork class and need an instance, use:

NeuralNetwork network = NeuralNetwork.loadFromFile(someFs);
// use network instance and have fun with it here

Note 1: I've defined a private constructor to force everyone use the loadFromFile factory method to create an instance. This means that this class can only be created from within a static method of this class.

Note 2: I've also rethrown the exception with an unchecked exception. This is personal taste. I wouldn't just log the exception and go on. Instead, I would throw the exception so that the caller handles it properly, because it doesn't make any sense to go on if an instance of the class hasn't been created. If you don't want to rethrow the exception as an unchecked one, just don't catch the original IOException and add a throws IOException clause to your factory method. This would force the callers of loadFromFile to catch the IOException and handle it.

Upvotes: 0

Andres
Andres

Reputation: 10717

this = (NeuralNetwork) ois.readObject();

Consider this as a hidden final argument that points to the instance that is executing the method. Its value cannot be changed. If you want to make a variable point to an instance, there's no problem with that, as long as you don't use this.

What you want to do is more appropriate for a factory or factory method than for a constructor. Use one of this patterns (or a static method if you want to keep it very simple) to create your instance from the file. Not sure if you need many instances of that class, but if you only need one, you should consider using a Singleton getInstance() method instead the previously mentioned.

Upvotes: 1

Keith Nordstrom
Keith Nordstrom

Reputation: 354

The keyword this is a read-only reference, you can never write this = even in the constructor. Moreover, the constructor in java does not return anything.

You would have to take the object you've read and map its properties one by one (or using reflection) to the properties you have in the object you're instantiating.

However, I would submit that by passing a file to a constructor and doing the IO in it you are violating separation of concerns. By writing things this way, you have forever tied a neural network to a File, with a whole host of attendant issues, including (not limited to) the fact that you may be storing your values elsewhere at some point.

IMO you are better off using a factory pattern to build your object and making your NeuralNetwork object a plain object. Then the ambiguity disappears because your factory method can simply return (NeuralNetwork) ois.readObject();

Upvotes: 1

Related Questions