Stuck in a weird problem...

Hi everyone. I'm a beginner to Java so my question may seem foolish and I apologize in advance! First of all, I have two .java files in a package (DateDriver.java and DateAD.java). I won't be able to explain this well in words so I'll just start off by pasting some code.

Here is my main method in DateDriver.java

publicvoid main(String[] args)

{

if (args.length == 0)

{

DateAD.DateAD();

}

Here is my DateAD method in DateAD.java

publicstaticvoid DateAD()

{

setCurrentDate();

}

And the method setCurrentDate that is also in DateAD.java

publicvoid setCurrentDate()

{

GregorianCalendar cal =new GregorianCalendar();

month = (short)cal.get(GregorianCalendar.MONTH);

year = (short)cal.get(GregorianCalendar.YEAR);

dayOfMonth = (short)cal.get(GregorianCalendar.DAY_OF_MONTH);

}

Alright! My main function in DateDriver.java can successfully invoke the method in DateAD.java however I am getting an error "non-static method setCurrentDate() cannot be referenced from a static context". I tried changing the methods from static to non-static and so forth but anyway I do it I always get an error similar to that one.

Is there a way I can get my main method in DateDriver.java to successfully invoke the DateAD method in DateAD.java and at the same time have the DateAD method successfully invoke setCurrentDate()?

Thanks!

[2046 byte] By [ApRiXa] at [2007-11-27 4:57:42]
# 1

Create a new DateAD object, then you can invoke it's instance methods. From static context (static methods) you can only invoke other classes' static methods unless you have an instance of the class.

i.e.

DateAD d = new DateAD();

d.setCurrentDate();

-Kayaman-a at 2007-7-12 10:13:16 > top of Java-index,Java Essentials,Java Programming...
# 2
Thanks! It worked really well. Edit : I fixed the problem that I mentioned here earlier.Message was edited by: ApRiX
ApRiXa at 2007-7-12 10:13:16 > top of Java-index,Java Essentials,Java Programming...
# 3

> What do I need to change from private to public to make these

> errors go away?

Have you tried anything? Like changing the variables to public that are mentioned in the errors?

edit: Ah, apparently you have tried something and it worked. congrats. :) Note that my remark below still stands.

It may be a better idea to create a toString() method in the DateAD class (that way you can keep your variables private, and thus encapsulated).

Message was edited by:

EvilBro

EvilBroa at 2007-7-12 10:13:16 > top of Java-index,Java Essentials,Java Programming...