Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

PLEASE HELP :(  RSS feed

 
Deniz Agah
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello , and thanks for anyone who would like to help !!
I have thread and its :

class gameThread extends Thread {
}

it has a big start and a big run function , thats why I am not copying them....
I want to call the thread in main, and I do call it like:

public void main(String[] args) {
gameThread gt1 = new gameThread();
gt1.start();
}
But unfortuneatly I get the error

non-static variable this cannot be referenced from a static context

pointing to the line:

gameThread gt1 = new gameThread();

Can someone please help me ?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

If gameThread (bad class name, by the way; Java class names should always start with a capital letter) is an inner class, then main() can't instantiate it without providing an instance of the enclosing class; that may be what the compiler is telling you here. If gameThread doesn't actually need the enclosing instance, then you can make it into a static member class -- add "static" before the "class" keyword in its declaration.

If gameThread is not an inner class, then I imagine the problem is in some other code you haven't shown us.

By the way: note that you normally don't override "start()" in a Thread subclass; if you do, make sure you call super.start() or the Thread won't work!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!