Results 1 to 10 of 10
  1. #1
    griffisu is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default 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.

  2. #2
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default 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);

  3. #3
    griffisu is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Problem with static context

    jlczuk i didn't specify any variable as Simple, it was declared exactly as you did (line 5).

  4. #4
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default 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.

  5. #5
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Problem with static context

    Removed by poster.
    Last edited by jlczuk; 04-24-2012 at 02:12 AM.

  6. #6
    griffisu is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default Re: Problem with static context

    Quote Originally Posted by jlczuk View Post
    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.

  7. #7
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default 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 ).
    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:

    Java 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:

    Java 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.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  9. #9
    griffisu is offline Member
    Join Date
    Apr 2012
    Posts
    4
    Rep Power
    0

    Default 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.
    The main purpose of the code was showing how to use the this reference.

    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 ?

  10. #10
    pbrockway2 is online now Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default 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 ?
    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.

Similar Threads

  1. Replies: 9
    Last Post: 06-10-2011, 01:40 AM
  2. Replies: 4
    Last Post: 03-14-2011, 01:05 AM
  3. String toCharArray problem - static context
    By Grendel0 in forum New To Java
    Replies: 10
    Last Post: 03-19-2010, 02:28 PM
  4. Replies: 1
    Last Post: 08-07-2007, 06:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 10:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •