Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Constructors

  1. #1
    zhider is offline Member
    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    0

    Default Constructors

    Hey guys, so on this project we are supposed to use constructor methods to print the date, right now I am getting the error that it cannot find the variable MoreDates on the code: printDate (MoreDates t); in the main method. Any ideas?

    Java Code:
    public class MoreDates {
    int year, month, day;
        /**
         * @param args the command line arguments
         */
    public MoreDates () {
          this.year= 0;
          this.month=0;
          this.day=0;  
    }
    public MoreDates(int year, int month, int hour){
        this.year=year;
        this.month=month;
        this.day= day;
        }
    public static void printDate (MoreDates t){
         MoreDates t1= new  MoreDates (2012, 11, 9);   
         System.out.println (t.month + ", " + t.day + ", "+ t.year);
    }
    public static void main(String[] args) {
         printDate (MoreDates t);    
        }
    }

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

    Default Re: Constructors

    You have mixed up the way methods are declared and how they are called. Your error is on line 21, where you have use the method declaration notation while you should have used a method call. The method printDate expects an instance of MoreDates (like you did on line 17, but that is the wrong place), so you have to create one first and use that as parameter.

  3. #3
    zhider is offline Member
    Join Date
    Oct 2012
    Posts
    37
    Rep Power
    0

    Default Re: Constructors

    So how would I change this code to make it work?

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

    Default Re: Constructors

    Read the tutorial that SurfMan links to to learn how to both declare and use methods, and then use this information to try to fix your code. If still stuck after this, show us your latest attempt.
    SurfMan likes this.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Constructors

    Quote Originally Posted by zhider View Post
    ... we are supposed to use constructor methods ...
    Just to set the terminology straight: constructors are constructors, methods are methods. In Java, there's no such thing as a constructor method.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    thinkinjava24 is offline Member
    Join Date
    Sep 2012
    Posts
    17
    Rep Power
    0

    Default Re: Constructors

    if you want to call a static method, the form is like this: ClassName.MethodName.
    regardless your logic, if you want to run your code, you should call the static method like what i said before, and your static method should take no argument. and in the method body, it should be "t1" instead of "t".
    to be honest, you really should go to learn the basic concept of method and constructor.
    have a nice programming day.

Similar Threads

  1. Constructors
    By EdOBannon in forum Advanced Java
    Replies: 2
    Last Post: 12-15-2011, 12:05 PM
  2. using constructors
    By droidus in forum New To Java
    Replies: 1
    Last Post: 09-18-2011, 11:30 PM
  3. constructors
    By droidus in forum New To Java
    Replies: 3
    Last Post: 04-28-2011, 09:14 PM
  4. Constructors
    By suresh.sa in forum New To Java
    Replies: 5
    Last Post: 10-20-2010, 01:10 AM
  5. constructors
    By khamuruddeen in forum New To Java
    Replies: 2
    Last Post: 12-01-2007, 04:15 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
  •