2

I have a class "A" which reads an XML file and does some processing. I put a method "load" in the constructor, but I'm wondering what happens if the XML file size is large and it takes time to be loaded.

class A
{
    public String fileName;

    A(String fileName)
    {
        this.fileName = fileName;
        load();
    }

    private load()
    {
        //here i load some xml file by given file name;
    }

    public searchByTag(String sometag)
    {
        //some search
    }

    public extractData()
    {
        //extract some data
    }
}  

For example if we have the following scenario:

A a = new A("somefile");
a.searchByTag("tag");
a.extractData();

The object "a" is created just after file is loaded, right?

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
adgfs
  • 423
  • 6
  • 17

4 Answers4

10

Yes, the thread executing that piece of code will go thru all the load before returning the instance of A.

Technically, the object "a" is created before the load (inside load you can safely refer to this), but it is assigned to the variable "a" only when the constructor return, which means when it has finished executing also the load() method.

Simone Gianni
  • 11,426
  • 40
  • 49
3

Since load() is called from constructor the instance construction will take as much time as it is needed to parse XML file. The constructor exits only when it is done, i.e. the object is ready. In your case only when the XML has been parsed.

Dominik Dorn
  • 1,820
  • 12
  • 18
AlexR
  • 114,158
  • 16
  • 130
  • 208
1

The control flow of the program doesn't return to the constructur call until all the code has been executed, except if errors happened, in which case an exception would be thrown.

If no errors happened the object would be created after the file is loaded, as you state.

Roberto Luis Bisbé
  • 2,080
  • 1
  • 15
  • 22
-1

The object will be created as soon as you have called the constructor but it won't be returned to you unless the load method has returned .

But the issue is the basic design is not right. You should not call the load method from the constructor .

Let the constructor just call the object and then call a load method on it to read the xml file. You need to have over loaded read methods in the class like this :

private load()
{
//here i load some xml file by given file name;
}

//for loading from default location
private load(String filePath)
{
//here i load some xml file by given file name;
}


private load(File file)
{
//here i load some xml file by given file name;
}

You get the idea !!. And then your calling program will have the flexibility of invoking the load method differently for different situations .

You should also have two-three different methods for first getting the file size and then claling a corresponding method.

  Have a look at this thread http://stackoverflow.com/questions/116574/java-get-file-size-efficiently  to get a better idea about an efficient way to deduce file size.
Anubis05
  • 1,234
  • 2
  • 13
  • 17