Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: How to call printDate method to output the dates?

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    35
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default How to call printDate method to output the dates?

    Here's my main file
    public static void main(String[] args) {
     
    		// Born Aug 21, 1996
    		int birthYear = 1996;
    		int birthDay = 21;
    		int birthMonth = 8;
     
    		Date date1 = new Date();
    		date1.year = birthYear;
    		date1.month = birthMonth;
    		date1.day = birthDay;
    		System.out.println (printDate(date1));
     
    		Date date2 = new Date(2013, 4, 9);
                    System.out.println (d.printDate.date2);
                            }
    The line with System.out.println is wrong. It is my attempt to call up the printDate method which is in a different file.
    Here's is my code for the different file
    public class Date {
        int year, month, day;
     
    	// "Empty" Constructor
    	public Date() {
     
    	}
     
    	// Construct date object using year, month, and day
    	public Date(int year, int month, int day) {
    		this.year = year;
    		this.month = month;
    		this.day = day;
    	}
            public static void printDate (Date d) {
                System.out.println (d.month + " " + d.day + ", " + d.year);
            }
    }


  2. #2
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: How to call printDate method to output the dates?

    I believe i answered this in your thread Rational program calling up from different file but in same package. Same issue, you're calling the static method incorrectly.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,106
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: How to call printDate method to output the dates?

    The line with System.out.println is wrong
    Please copy and post the full text of the error message that shows what the problem is and what statement it is on.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Apr 2013
    Posts
    25
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: How to call printDate method to output the dates?

    Hi,

    just call the static function using Date.printDate(date1); in place of System.out.println (printDate(date1));

Similar Threads

  1. Only if one method is true does it call the other?
    By ColeTrain in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 29th, 2012, 03:28 PM
  2. how could I output to a text area the output of a method
    By mia_tech in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 12th, 2012, 07:49 PM
  3. Replies: 3
    Last Post: October 31st, 2011, 12:42 AM
  4. How do I call a method from the main method?
    By JavaStudent1988 in forum Java Theory & Questions
    Replies: 5
    Last Post: October 19th, 2011, 08:37 PM
  5. Can i call init() method in destroy method.?
    By muralidhar in forum Java Servlet
    Replies: 1
    Last Post: October 22nd, 2010, 11:18 AM