• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

GeekWatch- error: static reference

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hiya again
I am stuck on GeekWatch- when trying to work out the no. of Milliseconds since Jan 1 1970. I have created an instance of the class Date and assigned this to variable date. I then try to initialise a variable , noOfMilliSecs (of type public long) using a method within the Date class with the following syntax:
I'm sorry but I had to remove the actual code
However I get the following message when I try and compile:
GeekWatch.java:45: Can't make a static reference to nonstatic variable noOfMilliSecs in class GeekWatch
Please can you help
Thanks
Yen

[This message has been edited by Johannes de Jong (edited August 20, 2001).]
 
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest you look at Thread1 & Thread2
Hope that helps
 
yen cheong
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Johannes
Thank you for your replies so far to all my Qs. I have read Thread 1 and Thread 2, but am confused. In my code, I have created an instance of the Date object, before running the ***() method (which is non-static) inside main. So I don't understand why I am getting this error message??!! Is there something that I am missing here?
Thanks in advance
Yen
Sorry I had to remove the name of the method. Part of the assignment is finding/using the correct method
[This message has been edited by Johannes de Jong (edited August 20, 2001).]
 
Johannes de Jong
tumbleweed
Posts: 5089
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you got your "call" to the method in this format : long xxx = object.method() ?
 
yen cheong
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Johannes
Wow ......... how did that work??!! Originally I had long xxx;
declared in the class but outside main, but it caused the error message!! I have now called the method as advised, and it doesn't cause a compilation error.
??? Please can you explain.
Regards
Yen-yei
 
yen cheong
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried declaring as both:
public long xxx;
and
private long xxx;
and they both still caused the compilation error
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because main() is static, you can't directly access any non-static variables initialized outside of main() (instance variables)unless you have an instance of your class, same thing goes for non-static methods.
Ways to correct this is:
1) Make your instance variables class variables by declaring them static (probably want to declare them private, also).
2) Instantiate an object of your class. This will allow you access the instance variables in main() via object.variable. For instance:

Hope this helps some,
Jason
[This message has been edited by jason adam (edited August 20, 2001).]
[This message has been edited by jason adam (edited August 20, 2001).]
 
yen cheong
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jason
Thanks for your reply, however I am still confused as I had already created an instance of the class inside of main. I had to declare the variable inside main to prevent the compilation error.
Regards
Yen
 
yen cheong
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Sorry I think I understand this now.
Regards
Yen
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One note on Jason's example -- it won't compile as is. You can't use "this" inside a static method.
 
jason adam
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bad Michael, yer absolutely correct about that. Been writing too many constructors lately and got all discombobulated.
Thanks!
Jason
 
Yeah, but does being a ninja come with a dental plan? And what about this tiny ad?
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic