Hello everyone, I'm having a really strange problem and I can't figure out why.

public void readEmpregados(ArrayList <Classe_Pessoa> list) throws IOException, ClassNotFoundException {
        FileInputStream file;
        try {
            file=new FileInputStream(this.empregados);
        }
        catch (FileNotFoundException e) {
            this.empregados.createNewFile();
            return;
        }

        System.out.println("I'm here");
        try {
            ObjectInputStream out=new ObjectInputStream(file);

            Object prototype=out.readObject();
            if (prototype instanceof ArrayList) {
                list=(ArrayList <Classe_Pessoa>)prototype; /*Faz cast para list*/
            }
        }
        catch (Exception e) {
            System.out.println("Something is wrong");
        }

        System.out.println("############Here's what I sent"+list);

This is supposed to read an ArrayList of "Classe_Pessoas" from a file and save it on the variable "list". It is working as intended. But for some reason, any changes I make to "list" are discarded as soon as this method returns.

If it's any help, list is private.

Recommended Answers

All 4 Replies

This is because you effectively pass a copy of reference and not the list when invoking the method. Read this.

commented: Very interesting +2

I recently changed the function in question to work in a more intuitive manner but I'm stuck on a different part of my program where I'm dependant on passing a value by reference. Is there a way I can force it?

Sorry if I seem rather lazy, it's just that unforseen circunstances have forced me to hurry completion of this project.

Edit: Seems it's working now for some reason, but I'm still curious to know if there is a way, just in case I face this problem again.

> but I'm still curious to know if there is a way, just in case I face this
> problem again

You can make your code work by passing in a list reference which already points to an existing List implementation. That way, even though you have a copy of the reference, it would in the end point to the same list which was passed. You'd need to change your Object de-serialization part of code to do an addAll() on the list passed by passing in the List read i.e.

if (prototype instanceof ArrayList) {
  list.addAll((ArrayList <Classe_Pessoa>)prototype);
}

You might want to add in a few null checks there though but yeah, that's how it can be done.

That being said, I'm still in favour of the method responsible for returning the data read in an appropriate container rather than the caller passing in a empty container to be filled by the method since your method puts undue responsibilities on the caller, something it isn't responsible for.

That being said, I'm still in favour of the method responsible for returning the data read in an appropriate container rather than the caller passing in a empty container to be filled by the method since your method puts undue responsibilities on the caller, something it isn't responsible for.

Indeed, that's what I did. Many thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.