All,

For the life of me I cannot get this program to compile. The error message "error: non-static variable this cannot be referenced from a static context" occurs on lines 23 and 24.

Here is the entire program:

public class ThreadTest {

    public static int x = 0;

    public class Counter implements Runnable {

        private int i;
        private int temp;

        public void run() {

            for (i=0; i<10; i++) {
                temp = x + 1;
                x = temp;
            }

        } //End run()

    } //End class Counter

    public static void main (String[] args){

        Counter myCounter1 = new Counter();
        Counter myCounter2 = new Counter();

        Thread threadA = new Thread(myCounter1);
        Thread threadB = new Thread(myCounter2);

        threadA.start();
        threadB.start();

        System.out.println("The final value of x is: " + x);

    } //End main()


} //End class ThreadTest

If anyone could get this working and explain what I'm doing wrong I'd appreciate it. I know the difference between static and non-static, but I'm just not seeing the problem here.

I'm trying implement the program illustrated here: http://www.medicalelectronicsdesign.com/sites/default/files/image/hobbs-fig4.jpg

And described as follows:

Here, two threads increment the global variable x. Reading the code, it appears that, depending how the threads interleave during their execution, x will have a value between 10 and 20 at the end of the program. The author has tested this program 10,000 times and, as expected, each time a value between 10 and 20 has resulted. For many years, this example was used as a class exercise—until an error was discovered. In fact, x can end up with values as small as 2.

The full article is here:
http://www.medicalelectronicsdesign.com/article/build-and-validate-safety-medical-device-software

Thanks for your help,
Bill

A non-static variable only exists when there is an instance of an object. If you have a static method, it can NOT get to any variable in a class object unless it has an instance of that class and uses that to get to the variable.
For your inner class, change public to static if you want the main method to be able to reference it.

Thanks for the reply. Your answer does solve the problem and the program now works as expected. Thank you!

Could you help me understand why it works?

I understand this: "A non-static variable only exists when there is an instance of an object."

And I understand this: "If you have a static method, it can NOT get to any variable in a class object unless it has an instance of that class and uses that to get to the variable."

Which of these rules am I violating on line 23 where I get the error message? I'm just trying to create an instance of class like I always do. What is the non-static variable that the compiler is complaining about?

Thanks,
Bill

you can't have two public classes in one file, you can have only one public class, which has the same name as your java file.

Which of these rules am I violating on line 23 where I get the error message? I'm just trying to create an instance of class like I always do. What is the non-static variable that the compiler is complaining about?

It's the inner class Counter, which is not static. So new Counter(); is trying to reference the non-static class Counter. This tutorial may help clarify this (to be honest, rather obscure) problem.
http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

The error basically says it all, you cant have a non static class and reference a static variable and the same goes you cant have a non static class and then try and get reference to a static variable.

to fix your problem cahnge the 'public class Counter.....' to:

public static class Counter implements Runnable

This will make both the location you are refering from: main() and the class Counter static(only one exists)

you can't have two public classes in one file, you can have only one public class, which has the same name as your java file.

... except for inner classes (as is the case in this thread).

Thank you to all that replied.

James, That tutorial explains everything and was extremely helpful, thank you for pointing me to it.

I appreciate your help folks... the problem is solved and I understand why.

Regards,
Bill

but when the inner class is a seperate class like this

class aClass{
    private int isAccessible;
    aClass(int a){
        isAccessible = a;
    }
    aClass(){this(0);}
    public int get(){ return isAccessible;}
    public int getDiff(aClass a){ return a.isAccessible;}
}

public class MyClass {
    public static void main(String args[]) {
       aClass frst,Scnd;
       frst = new aClass();
       Scnd = new aClass(5);
       System.out.println("frst int  " + frst.get() + "\nScnd int   " + Scnd.get() +"\nScnd threw frst   "+ frst.getDiff(Scnd) );
    }
}

compiler doesn't give error although now also we are calling non-static class from a static method. why?

Your code does not have an inner class. Only nested classes can be inner classes.

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.