Okay, this puzzle the hell out of me.

I've got 2 different sets of code, one which works, one which doesn't. They are both very similar, and I can't see what is causing the problem.

Code that works:

public static void main(String[] args) {
        PropertyConfigurator.configure("log4j.properties");
        new Settings().globalSettings(args[0]);
        logger.info("Read settings from " + args[0]);
        while (true) {
            logger.info("Starting main scanning loop.");
            /*
             * Get a list of jobs, then iterate through them, get their settings,
             * and pick up files for them to process.
             *
             * Assumptions:
             *      1. There are jobs in the settings file
             */
            String[] jobList = new Settings().jobList(args[0]);
            logger.info("Job found");

            for (int numJobs = 0; numJobs < jobList.length; numJobs++) {
                /*
                 * Get each job's settigns, then call filePickup() for it
                 */
                String jobName = jobList[numJobs];
                new Settings().jobSettings(args[0], jobName);

                logger.info("Starting job " + jobName);
                try {
                    Thread.sleep(6000);
                } catch (InterruptedException ie) {
                }
                new MultiThreader().scan();
            }
        }
    }

    void scan() {
        /*
         * This function makes it possible to track the total amount of threads
         */
        if (!MultiThreader.priorityDirs.equals("") && !MultiThreader.priorityDirs.equals(null)) {
            logger.info("Scanning high priority files");
            /*
             * Scan priority folder for files, use filepickup
             * filePickup will scan the high priority dir(s)
             */
            filePickup("high", MultiThreader.priorityThreads);
        } else {
            MultiThreader.normalThreads = MultiThreader.totalThreads;
        }

        if (!MultiThreader.normalDirs.equals("") && !MultiThreader.normalDirs.equals(null)) {
            logger.info("Scanning low priority files");
            /*
             * Scan normal folders for files, use filepickup
             * filepickup will scan the low priority dir(s)
             */
            filePickup("low", MultiThreader.normalThreads);
        }
    }

Code that doesn't work:

public static void main(String[] args) {
        PropertyConfigurator.configure("log4j.properties");
        new Settings().globalSettings(args[0]);
        logger.info("Read settings from " + args[0]);
        while (true) {
            logger.info("Starting main scanning loop.");
            /*
             * Get a list of jobs, then iterate through them, get their settings,
             * and pick up files for them to process.
             *
             * Assumptions:
             *      1. There are jobs in the settings file
             */
            String[] jobList = new Settings().jobList(args[0]);
            logger.info("Job found");

            for (int numJobs = 0; numJobs < jobList.length; numJobs++) {
                /*
                 * Get each job's settigns, then call filePickup() for it
                 */
                String jobName = jobList[numJobs];
                new Settings().jobSettings(args[0], jobName);

                logger.info("Starting job " + jobName);
                try {
                    Thread.sleep(6000);
                } catch (InterruptedException ie) {
                }

                new MultiThreader().jobSpawner(this, jobName); [B]// Error on this line[/B]
            }
        }
    }

    void jobSpawner(MultiThreader mt, String jobName) {
        new Thread(new JobThread(mt, jobName)).start();
    }

The error is "java.lang.RuntimeException: Uncompilable source code - non-static variable this cannot be referenced from a static context"

Please help me out here, this is extremely frustrating.

new MultiThreader().jobSpawner(this, jobName);

'this' is a key word that can be used in a class only by its class members and it used to call class variables and methods:

private int a = 0;

public void setA(int a) {
  System.out.println(this.a); // it will print the value of the 'a' class variable
  System.out.println(a); // it will print the argument.

  this.a = a; // it will set the value of the class variable to be the argument
}

You cannot use the 'this' in a static method like the static void main()

In the main method you need to pass a instance of that class

I don't know your logic, but what if you try this:

class MultiThreader {

    main(...) {

 
       new MultiThreader().jobSpawner(jobName);
    }

    void jobSpawner(String jobName) {
        new Thread(new JobThread(this, jobName)).start(); // use 'this' here
    }

}

In this case the 'this' would reference the instance that is created in the main:
new MultiThreader().jobSpawner(jobName)

Edited 6 Years Ago by javaAddict: n/a

There have been a few threads on static vs. instance methods and fields recently. You might find those worth a look. Very important underlying ideas, if you don't understand static and instance you're going to have a lot of trouble.

This question has already been answered. Start a new discussion instead.