Results 1 to 6 of 6
Thread: Constructors
- 11-11-2012, 11:08 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 37
- Rep Power
- 0
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); } }
- 11-11-2012, 11:25 PM #2
Godlike
- Join Date
- Nov 2012
- Posts
- 194
- Rep Power
- 1
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.
- 11-12-2012, 12:01 AM #3
Member
- Join Date
- Oct 2012
- Posts
- 37
- Rep Power
- 0
Re: Constructors
So how would I change this code to make it work?
-
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.
- 11-12-2012, 06:06 AM #5
- 11-13-2012, 03:43 AM #6
Member
- Join Date
- Sep 2012
- Posts
- 17
- Rep Power
- 0
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
-
Constructors
By EdOBannon in forum Advanced JavaReplies: 2Last Post: 12-15-2011, 11:05 AM -
using constructors
By droidus in forum New To JavaReplies: 1Last Post: 09-18-2011, 10:30 PM -
constructors
By droidus in forum New To JavaReplies: 3Last Post: 04-28-2011, 08:14 PM -
Constructors
By suresh.sa in forum New To JavaReplies: 5Last Post: 10-20-2010, 12:10 AM -
constructors
By khamuruddeen in forum New To JavaReplies: 2Last Post: 12-01-2007, 03:15 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks