Search code examples
javaclassprogram-entry-pointrunnable

Access one class's members after running it from another class


I recently wrote a class that implements the run method and then parses a video file while grabbing meaningful information. Now I've created a new class that performs a similar operation on the same file but uses a different method of parsing while grabbing other meaningful information. Long story short, I'm required to use two different methods of parsing because some data cannot be extracted by one and some cannot be extracted by the other. The problem I'm facing is that both classes implement the run method, but now I need to start the new class, grab information, start the other class, grab information, then compare the information and print it to the console. This is the gist of what I'm trying to do:

public class first {
    [public member variables]
    ....
    public void run(String[] args) {
        // parse the file from args and store data
    }

    public static void main(String[] args) {
        new first().run(args); // <------ A
    }
}

public class second {
    [public member variables]
    ....
    public void run(String[] args) {
        // parse the file from args and store data
    }

    public static void main(String[] args) {
        new second().run(args);
    }
}

What I'm trying to do is call the first class's main method in order to keep a reference to the class and grab the data from it when it's finished. So I added something like this in the second class:

public class second {
    [public member variables]
    first firstClass;
    int dataFromFirst = 0;
    ....
    public void run(String[] args) {
        // parse the file from args and store data
        firstClass = new first();
        firstClass.main(args); // <------ B
        dataFromFirst = firstClass.getSomeData(); // <------ C
    }

    public static void main(String[] args) {
        new second().run(args);
    }
}

When I start the second class, everything runs fine, the parser does it's job with both the second and first classes, but when I try to extract the data found by the first class, it's null. I figure once the first class's main method finishes after 'B', once 'A' goes out of scope, everything from the first class is lost. So when I try to get data at 'C', there's nothing there. If this is the case, is there any way I can access the first class's data before it's lost?

I don't have as much knowledge about multithreaded programs so this may just be a very simple solution that I've never seen before.


Solution

  • The reason this doesn't work is that each main method creates its own instance of the class and uses it locally. This has nothing to do with threads, and in fact as far as I can tell your program doesn't actually use multithreading at all.

    To fix it, don't call from one main method to the other. In fact, don't even have two main methods in the first place, there's almost never a reason to have more than one. Instead, simply call run directly, like this:

    public void run(String[] args) {
        // parse the file from args and store data
        firstClass = new first();
        firstClass.run(args);
        dataFromFirst = firstClass.getSomeData();
    }