Results 1 to 11 of 11
- 08-19-2013, 02:29 PM #1
Member
- Join Date
- Aug 2013
- Posts
- 6
- Rep Power
- 0
Compiler thinks non-static method is static
Hello people
As the title says itself, the compiler (I'm using JCreator) thinks a certain context is static while it's not, the error that comes with this is that a non-static variable cannot be declared in that method due to it not being static.
I've checked all the document and nowhere stands static but in a few comments.
Let me give a file as an example
Java Code:public class A1 { InfoTracker saveInfo; public A1() { } public String process(InfoTracker info) { String tempScenario, text; tempScenario = ""; switch(info.getScenario()) { default: text = "This action is not yet supported."; tempScenario = ""; } info.setPreviousScenario(info.getScenario()); info.setScenario(tempScenario); saveInfo = info; info.setPreviousText(GUI.mainInfo.getText()); return text; } public InfoTracker getInfo() { return saveInfo; } }
(Build Output)
Java Code:C:\Users\Dennis\Dropbox\Install\JCreator LE\MyProjects\Java in TWO semesters\Own Made\Interactive novel\Control Buttons\A1.java:31: error: non-static variable mainInfo cannot be referenced from a static context info.setPreviousText(GUI.mainInfo.getText()); ^
Java Code:error: non-static variable mainInfo cannot be referenced from a static context
Java Code:public JTextArea mainInfo = new JTextArea();
Help would be much appreciated wince there's a certain error if I would make everything static (process() is not the only method that has this problem, pretty much every method that requests a variable from another class).
Thank you for reading,
~Den3107
P.S. Don't comment about what this class does, it's all a work in progress
P.S.S. GUI is a classLast edited by Den3107; 08-19-2013 at 02:42 PM.
- 08-19-2013, 02:34 PM #2
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Compiler thinks non-static method is static
its not the mainInfo that is the problem, it is the fact that you're using
Java Code:GUI.mainInfo
BTW: JCreator is your IDE, not the compiler. Don't confuse the JDK with the pretty graphical tool you're using, JCreator is not Java."Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-19-2013, 02:37 PM #3
Member
- Join Date
- Aug 2013
- Posts
- 6
- Rep Power
- 0
Re: Compiler thinks non-static method is static
Thank you for the quick reply
I expect there's no solution except making it static? Since I have problems with saving and loading the save files if I have to make everything static.
~Den3107
Edit: Yes, GUI is a class.
Edit: Also, I can then understand I shouldn't need to request public variables in that fashion, but why does it also give the error if I request the variable in the fashion of a method within a class that returns it?
Example:
Java Code:public String getMainInfoText() { return mainInfo.getText(); }
Last edited by Den3107; 08-19-2013 at 02:42 PM.
- 08-19-2013, 02:37 PM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Compiler thinks non-static method is static
So what is GUI defined as? You need to show more of your code.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 08-19-2013, 02:41 PM #5
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Compiler thinks non-static method is static
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-19-2013, 02:46 PM #6
Member
- Join Date
- Aug 2013
- Posts
- 6
- Rep Power
- 0
- 08-19-2013, 03:02 PM #7
Member
- Join Date
- Aug 2013
- Posts
- 6
- Rep Power
- 0
Re: Compiler thinks non-static method is static
Nevermind, I have found the solution
The problem was that it always makes it a static unless you make the class the called method is in as a variable
So the new code would be:
Java Code:public class A1 { InfoTracker saveInfo; public A1() { } public String process(InfoTracker info) { GUI gui = new GUI(); // the new line String tempScenario, text; tempScenario = ""; switch(info.getScenario()) { default: text = "This action is not yet supported."; tempScenario = ""; } info.setPreviousScenario(info.getScenario()); info.setScenario(tempScenario); saveInfo = info; info.setPreviousText(gui.getMainInfoText()); // note how the text between the first bracelets has changed return text; } public InfoTracker getInfo() { return saveInfo; } }
Java Code:public String getMainInfoText() { return mainInfo.getText(); }
Last edited by Den3107; 08-19-2013 at 03:05 PM.
- 08-19-2013, 03:06 PM #8
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Compiler thinks non-static method is static
Your latest code is weird. First of all I expect that you would be doing this:
Java Code:info.setPreviousText(gui.mainInfo.getText());
Java Code:info.setPreviousText(gui.getMainInfo().getText());
So please don't tell me that you made mainInfo static..."Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-19-2013, 03:07 PM #9
Member
- Join Date
- Aug 2013
- Posts
- 6
- Rep Power
- 0
- 08-19-2013, 03:08 PM #10
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Compiler thinks non-static method is static
Oops, right you are. Thumbs up!
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-19-2013, 03:11 PM #11
Member
- Join Date
- Aug 2013
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
Non static method printBoard(boolean) cannot be referenced from a static context
By warp in forum New To JavaReplies: 3Last Post: 05-31-2012, 07:56 PM -
Error in Code: Non-static method cannot be referenced from a static context
By oaklandsbest in forum New To JavaReplies: 9Last Post: 06-10-2011, 12:40 AM -
Can't make static reference to non-static method -> huh?! Simple car prgm
By enerj in forum New To JavaReplies: 7Last Post: 09-24-2010, 05:09 AM -
Java class HashIt with a static recursive method and a static iterative method
By kezkez in forum New To JavaReplies: 3Last Post: 02-09-2010, 05:22 AM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 05:05 AM
Bookmarks