-1

Okay... so I've done a lot of trial and error... and I can't seem to call my usFormat method to my main and I think I've pretty much messed up the entire thing lol.. any help would be nice. Just... please on the beginner level.

 class Date {
     String date;
     String day;
     String month;
     String year;
     StringTokenizer st;
     Scanner sc = new Scanner (System.in);


     //instance vars go here
     public Date (String date){
         while (sc.hasNextLine()) {
             st = new StringTokenizer (sc.nextLine());
             this.month =  st.nextToken();
             this.day   =  st.nextToken();
             this.year  =  st.nextToken();
        }

        } //end constructor

        public String usFormat () {
            return month + " " + day + "," + year;
        } //end usFormat
        public String euFormat () {
            return null;
        } //end euFormat

        public static void main (String[] args){
            Date usFormat = new Date (date);

        }
    } 
noahlz
  • 10,202
  • 7
  • 56
  • 75
  • -1. Please *google* your error message, this is covered in countless SO questions and tutorials online. – djechlin May 24 '13 at 17:02
  • 2
    Here is your exact same problem: http://stackoverflow.com/questions/7379915/java-how-to-call-non-static-method-from-main-method – djechlin May 24 '13 at 17:03
  • 1
    @all answerers, please close as duplicate instead of duplicating answers. – djechlin May 24 '13 at 17:03

8 Answers8

2

Since your calling a method from a static method, you'll need to make that method (usFormat() and euFormat()) static too.

public static String ...

Static basically means you don't need an instance of the class. So.. You don't need an instance to run main but you do need an instance to call usFormat() (since it is not static). That is not going to work. Hence the error.

In case you don't want to make these methods static, consider moving your code out of the main class and into another. You can create an instance of this class using new in main (this would work with the given class too, in case you want too (new Date()).usFormat()).

Menno
  • 12,175
  • 14
  • 56
  • 88
1

You don't call a method on an object by declaring a variable as the same name as the method. This is how you would call the method (and output it).

Date d = new Date("a string");  // You don't seem to be using the argument anyways.
String formattedDate = d.usFormat();
System.out.println(formattedDate);

Additionally, you don't even use the argument to your Date constructor. Just remove the String date parameter to it, and you can remove "a string" when you call the constructor.

rgettman
  • 176,041
  • 30
  • 275
  • 357
0

You defined an instance class named Date so you just need to do the following:

    public static void main (String[] args){
        Date usFormat = new Date (date);
        System.out.println(date.usFormat());
    }

No need to make static any methods of your Date class.

noahlz
  • 10,202
  • 7
  • 56
  • 75
0

Another approach would be to create an instance of Date inside your main method, for example myDate = new Date(""); and then do myDate.usFormat();

Giannis
  • 5,286
  • 15
  • 58
  • 113
0

In order to call a non-method from a static method, you have to do one of the following:

1) Instatiate the class where the non-static method is defined.

Date usFormat = new Date (date);
System.out.println(date.usFormat());

2) Make the non-static method static

public static .... 

I would suggest you use the first option, since the second one requires much more re-factoring.

Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147
0

Non-static methods are also called instance method, i.e. they require instance of class. Instance or object is something that you create using keyword new. The example is that class is like a car and object is like the specific car that you drive.

So, to call the method you have to do the following:

  public static void main (String[] args){
    Date usFormat = new Date (date);
    String formattedDate = usFormat.euFormat();
  }
AlexR
  • 114,158
  • 16
  • 130
  • 208
0

In your code there isn't any problems with static/not static methods. Only some typos:

 class Date {
 String date;
 String day;
 String month;
 String year;

 Scanner sc = new Scanner (System.in);


 //instance vars go here
 public Date (String date){
         StringTokenizer st= new StringTokenizer (date, " ");
         this.month =  st.nextToken();
         this.day   =  st.nextToken();
         this.year  =  st.nextToken();

    } //end constructor
    public String usFormat() {
        return month + " " + day + "," + year;
    } //end usFormat
    public String euFormat() {
        return null;
    } //end euFormat
    public static void main (String[] args){
      Date usFormat = new Date ("20 10 2013");    
    }
} 

However, about your question, for calling a static method you don't need an instance of the class.

jfabrizio
  • 760
  • 7
  • 15
-2

define the date as static. static String date; you can't refer non statics from static method main

Sanjaya Liyanage
  • 4,706
  • 9
  • 36
  • 50