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 ).
Quote:
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 );
}
}
}
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:
Code:
SimpleTime time = new SimpleTime (15 , 30, 19);
Re: Problem with static context
jlczuk i didn't specify any variable as Simple, it was declared exactly as you did (line 5).
Re: Problem with static context
Hmmm, I was going by what you typed:
Quote:
ThistTest.java:5: error: non-static variable this cannot be referenced from a static context
Simple time = new SimpleTime (15 , 30, 19);
^
1 error.
Re: Problem with static context
Re: Problem with static context
Quote:
Originally Posted by
jlczuk
Hmmm, I was going by what you typed:
already changed that in the original post.
jlczuk but i can't do that, the declaration of main must be like that.
One thing, the code is not mine, it's from the book.
Re: Problem with static context
Quote:
I would be very grateful if somebody can shed some light in why the code does not work (and Deitel says it does ).
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.
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:
Code:
static class SimpleTime
{
private int hour;
// etc
}
If SimpleTime is declared as static no enclosing instance is required. (Instead the context comes from the ThisTest class itself and any static state it has.)
Another might be:
Code:
public static void main ( String[] args )
{
SimpleTime time = new ThisTest().new SimpleTime( 15, 30, 19);
System.out.println( time.buildString() );
// etc
(with SimpleTime nonstatic as originally). In this case a new instance of ThisTest is created specifically to provide the context for the SimpleTime instance that is also created.
-----
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.
Re: Problem with static context
Thanks very much for the explanation pbrockway2!
Quote:
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.
The main purpose of the code was showing how to use the this reference.
Quote:
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.
I'm always using separate files to deal with classes, the thing about the code above was that i couldn't find a explanation why it did not work, which you provided.
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 ?
Re: Problem with static context
Quote:
could you recommend some place where i could read more about this kind ( SimpleTime time = new ThisTest().new SimpleTime( 15, 30, 19); ) of declaration ?
There is something similar (under the heading "Instantiating") on Roedy Green's glossary page. That page (and the whole glossary) is a good go-to place for understandable (and, as far as I know, accurate) explanations of things.