Results 1 to 10 of 10
Thread: Problem with static context
- 04-24-2012, 01:28 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
Problem with static context
I'm having some problems in understanding why the code below do not work, I typed it exactly as the book ( Java How to Program - Ninth Edition, Deitel, chapter 8 figure 8.4. ).
I get the following message when I compile it:
ThistTest.java:5: error: non-static variable this cannot be referenced from a static context
SimpleTime = new SimpleTime (15 , 30, 19);
^
1 error.
One solution is to split the code in two files, ThistTest.java and SimpleTime.java, but why does it work while the code do not?
The second solution is to put static before the declaration of the class SimpleTime, but i've learned to use static only in methods and variables and i don't want to declare SimpleTime as static ( and what will happen to SimpleTime attributes and methods if declare it as static ? )
I would be very grateful if somebody can shed some light in why the code does not work (and Deitel says it does ).
Java Code:public class ThisTest { public static void main ( String[] args ) { SimpleTime time = new SimpleTime( 15, 30, 19); System.out.println( time.buildString() ); } class SimpleTime { private int hour; private int minute; private int second; public SimpleTime(int hour, int minute, int second) { this.hour = hour; this.minute = minute; this.second= second; } public String buildString() { return String.format("%24s: %s\n%24s: %s", "this.toUniversalString()", this.toUniversalString(), "toUniversalString()", toUniversalString() ); } public String toUniversalString() { return String.format( "%02d:%02d:%02d", this.hour, this.minute, this.second ); } } }
Last edited by griffisu; 04-24-2012 at 01:54 AM.
- 04-24-2012, 01:33 AM #2
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
Re: Problem with static context
The type of the variable you specified as Simple and the constructor you called is for SimpleTime. If SimpleTime does not extend or implement the Simple class (which in your code it does not), this will not work. My guess is that your have a type and what you really wanted was:
Java Code:SimpleTime time = new SimpleTime (15 , 30, 19);
- 04-24-2012, 01:43 AM #3
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
Re: Problem with static context
jlczuk i didn't specify any variable as Simple, it was declared exactly as you did (line 5).
- 04-24-2012, 01:48 AM #4
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
Re: Problem with static context
Hmmm, I was going by what you typed:
ThistTest.java:5: error: non-static variable this cannot be referenced from a static context
Simple time = new SimpleTime (15 , 30, 19);
^
1 error.
- 04-24-2012, 01:50 AM #5
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
Re: Problem with static context
Removed by poster.
Last edited by jlczuk; 04-24-2012 at 02:12 AM.
- 04-24-2012, 01:58 AM #6
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
- 04-24-2012, 01:59 AM #7
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Re: Problem with static context
I would be very grateful if somebody can shed some light in why the code does not work (and Deitel says it does ).
main() is a "static context" in the sense that there is no instance of ThisTest associated with it. Indeed under some circumstances the JVM will run the code in main() before any instances of anything have been created.
Within ThisTest you declare a (non static) inner class SimpleTime. Such inner classes *are* always associated with an instance of the enclosing type (ThisTest). In effect a ThisTest instance will provide the context for a specific type it encloses. And this context is required.
The "this" that the compiler is talking about is the instance of ThisTest that is required for the inner class (SimpleTime) to make sense. And within the static main() method there is no associated "this".
-----
Variations on this code will compile. Eg:
Java Code:static class SimpleTime { private int hour; // etc }
Another might be:
Java Code:public static void main ( String[] args ) { SimpleTime time = new ThisTest().new SimpleTime( 15, 30, 19); System.out.println( time.buildString() ); // etc
-----
Unless you really want nested/inner classes for some reason (and that reason is elaborated sufficiently to indicate why you want the inner class to be static or not), stick to two files as you suggest in the OP.
-
Re: Problem with static context
Ignore the recommendation to get rid of static from main as that won't help.
Put SimpleTime in its own file.
- 04-24-2012, 02:30 AM #9
Member
- Join Date
- Apr 2012
- Posts
- 4
- Rep Power
- 0
Re: Problem with static context
Thanks very much for the explanation pbrockway2!
I haven't read the Deitel book and, hence, don't know what the code is supposed to do and can't comment on its "working" or not. But it won't compile.
Unless you really want nested/inner classes for some reason (and that reason is elaborated sufficiently to indicate why you want the inner class to be static or not), stick to two files as you suggest in the OP.
Thanks again for your clear explanation and attention, could you recommend some place where i could read more about this kind ( SimpleTime time = new ThisTest().new SimpleTime( 15, 30, 19); ) of declaration ?
- 04-24-2012, 03:19 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,716
- Rep Power
- 19
Re: Problem with static context
could you recommend some place where i could read more about this kind ( SimpleTime time = new ThisTest().new SimpleTime( 15, 30, 19); ) of declaration ?
Similar Threads
-
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, 01:40 AM -
Yet another "non-static method cannot be referenced fro a static context" problem
By jazzermonty in forum New To JavaReplies: 4Last Post: 03-14-2011, 01:05 AM -
String toCharArray problem - static context
By Grendel0 in forum New To JavaReplies: 10Last Post: 03-19-2010, 02:28 PM -
Error: Non-static method append(char) cannot be referenced from a static context
By paul in forum Advanced JavaReplies: 1Last Post: 08-07-2007, 06:05 AM -
Error: non-static variable height cannot be referenced from a static context at line
By fernando in forum AWT / SwingReplies: 1Last Post: 08-01-2007, 10:25 PM
Bookmarks