0

Receiving error non static variable this cannot be referenced from a static context when compiling the following code; what is the problem?

class bwCalc {
    class Tst{
     public void tst() {
         byte[] data = new byte[1024];//1 kb buffer
         int count;
         long startedAt = System.currentTimeMillis();
         while((System.currentTimeMillis()-startedAt)<1) {
             System.out.println("hello\n");
         }

     }
 }
 public static void main(String argc[]) {
     Tst c = new Tst();
     c.tst();  
 }
}
JoshDM
  • 4,939
  • 7
  • 43
  • 72
Jinu Joseph Daniel
  • 5,864
  • 15
  • 60
  • 90
  • 4
    your exact error - http://stackoverflow.com/questions/7638169/non-static-variable-this-cannot-be-referenced-from-a-static-context – djechlin Feb 22 '13 at 18:32

2 Answers2

4

You need the Outer class instance.

bwCalc  b = newbwCalc ();     
     Tst c = b.new Tst();
         c.tst();  

Or simply make the inner class static.

PermGenError
  • 45,977
  • 8
  • 87
  • 106
3

The Tst class should be static, because it's not attached to a particular instance of bwCalc.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413