java question (compilation problem)

notfred

Lifer
Feb 12, 2001
38,241
4
0
I have this line of code:

long start = Calendar.getTimeInMillis();

the class imports java.util.* which includes the Calendar class.

When I try to compile it, I get the error:

"non-static method getTimeInMillis() cannot be referenced from a static context"

WTF? Nothing I have declared is static... why is it doing this?
 

PrincessGuard

Golden Member
Feb 5, 2001
1,435
0
0
getTimeInMillis() is not a static method, which means you can't use Calendar.getTimeInMillis(). You have to instantiate a Calendar object and call it from there.
 

MrChad

Lifer
Aug 22, 2001
13,507
3
81
Originally posted by: PrincessGuard
getTimeInMillis() is not a static method, which means you can't use Calendar.getTimeInMillis(). You have to instantiate a Calendar object and call it from there.

Yep. You need to do:

Calendar myCal = new Calendar();
long start = myCal.getTimeInMillis();
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
Yeah... I jsut figured it out... and i tried it, and now it bitches about getTimeInMillis being protected. How the hell to I access a protected method of a class I didn't write?
 

arcain

Senior member
Oct 9, 1999
932
0
0
Use java.long.System.currentTimeMillis()

It is static, and you can call it like in your first post.