Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Java Non-static variable cannot be referenced from a static context

I'm trying to use a variable from one class file inside another class file.
The original variable is public boolean keyFound = false;
When I try to access the variable I use Event01.keyFound;
it gives me an error. I tried using a getter method that returns this.keyFound and it gives me the same error only it says non-static method cannot be referenced from a static context.
 
I'm trying to use a variable from one class file inside another class file.
The original variable is public boolean keyFound = false;
When I try to access the variable I use Event01.keyFound;
it gives me an error. I tried using a getter method that returns this.keyFound and it gives me the same error only it says non-static method cannot be referenced from a static context.
simple solution to this: just add the 'static' keyword to your variable, OR, rather than making it a global variable make it a local variable
 
Thank you. That gets the variable working. I now have a bigger problem with the code where it worked yesterday, now it gives me an error saying:

Exception in thread "main" java.lang.NoClassDefFoundError: awesomequest3/UI
at awesomequest3.GameManager.<init>(GameManager.java:10)
at awesomequest3.GameManager.main(GameManager.java:18)
Caused by: java.lang.ClassNotFoundException: awesomequest3.UI

I've been following this tutorial:
To view this content we will need your consent to set third party cookies.
For more detailed information, see our cookies page.
and it worked yesterday:

AwesomeQuest3
 
I think you need to have import UI; at the beginning of GameManager.java. Although that does not sit well with your claim that it worked yesterday. What have you changed since yesterday then, apart from implementing @Antero360 's suggestion ?
 
I think you need to have import UI; at the beginning of GameManager.java. Although that does not sit well with your claim that it worked yesterday. What have you changed since yesterday then, apart from implementing @Antero360 's suggestion ?
Amazingly I moved the event03 and event02 out of the project folder and commented out the code and the game worked. Then I put the code and files back in and it works. An unplug it and plug it back in fix :/
Also I was able to access the non-static variable by using gm.ev1.keyFound so it doesn't have to be global :D
The key from screen 1 can now be used on screen 3 and the game works. Thank you for the help @cbreemer and @Antero360
 
That is quite baffling... but stranger things happen 😀
Good that you gave up on the global. When you need to access stuff from another class, best use a getter/setter function.
 
Amazingly I moved the event03 and event02 out of the project folder and commented out the code and the game worked. Then I put the code and files back in and it works. An unplug it and plug it back in fix :/
Also I was able to access the non-static variable by using gm.ev1.keyFound so it doesn't have to be global :D
The key from screen 1 can now be used on screen 3 and the game works. Thank you for the help @cbreemer and @Antero360
Step 0: sir/ma'am, have you unplugged it and plugged it back in? lol
 

Buy us a coffee!

Back
Top Bottom