Results 1 to 2 of 2
  1. #1
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Static error with toString in method

    I am trying to write a program that gathers the date and advances it to tomorrow's date. I need to use the toString method for my output but keep getting an error saying something like cannot use this in static context. Is it not possible to use the toString method to access a different static method in the same class?

    Java Code:
    public class Date 
    {
    	static String todayDate;
    	static String newDate;
    	
    	public static void main(String[] args) throws ParseException 
    	{
    	    
    
    		advance();		
    
    		
    	}
    	
    	public static  void advance()
    	{
    		java.util.Date today = Calendar.getInstance().getTime();
    	    SimpleDateFormat date = new SimpleDateFormat("m/d/yyyy");
    	    todayDate = date.format(today);
    		System.out.println("Today's date is " + todayDate);
    		
    		Calendar tomorrow = Calendar.getInstance();
    		tomorrow.add(Calendar.DATE, 1);
    		SimpleDateFormat format1 = new SimpleDateFormat("m/d/yyyy");
    	    newDate = format1.format(today);
    	    System.out.printf("Tomorrow's date is %s", this);
    		
    	}
    	
    	public String toString()
    	{
    		return String.format("m/d/yyyy", newDate);
    		
    	}
    	
    }
    Thanks

  2. #2
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    727
    Rep Power
    2

    Default Re: Static error with toString in method

    Non-static methods, like your toString() method, only work on instances. You never create an instance of Date (Bad choice of name by the way). Try to avoid running everything inside the static main and other static methods. Create an instance of your object and then play around with the instance. Note that advance() is also a non-static method in my example, since it works on the same instance.

    This would be a better approach:

    Java Code:
    public class DateAdvancer {
        private String todayDate;
        private String newDate;
         
        public static void main(String[] args) throws ParseException {
             DateAdvancer da = new DateAdvancer();
             da.advance();
             System.out.println(da.toString());
        }
         
        public void advance() {
           ...snip...
        }
         
        public String toString() {
            return String.format("m/d/yyyy", newDate);
        }
    
    }
    Last edited by SurfMan; 09-08-2013 at 10:36 AM.

Similar Threads

  1. Replies: 9
    Last Post: 06-10-2011, 12:40 AM
  2. Problems with Static toString() method
    By RBNSN in forum New To Java
    Replies: 6
    Last Post: 07-09-2010, 10:15 AM
  3. toString() method error?
    By blueduiker in forum New To Java
    Replies: 6
    Last Post: 02-07-2010, 03:19 AM
  4. Replies: 1
    Last Post: 02-10-2009, 10:03 AM
  5. Replies: 1
    Last Post: 08-07-2007, 05:05 AM

Posting Permissions

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