• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Load on startup

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
I wish to Load a file into an array on start up. I had the file loading into my array when the user clicked a button called Load, so I know that it works.
In an attempt to get this to work on startup I put the I/O loading code that was in the Load button into "private void loadFile(String filename)".
I then edited my main method to this:

I'm getting the following error when I compile:

loadFile("Books1.ser");
^
1 error
Please help,
Berty
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is loadFile() static? If not, you can't call it from a static context (like the main method). You must have an instance on which to call it.
 
Berty Ahern
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eh whats static?
it's non static. How should I change it to static?
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How did you make the main method static?
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, why would you want to do the I/O on the GUI thread? Even if it were initiated by a "Load" button, you'd still want to do it on some other thread, so as not to freeze the GUI during the loading. If you did it on the main thread here, you could multitask -- the I/O would happen while the GUI was being set up.
 
Berty Ahern
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
createAndShowGUI() is static. So does that make my main method static also?
How might i run the loadFile methed outside the main thread?
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember, Ernest, that this is the beginner forum. They might not (and I'm sure they don't if they don't know about statics) understand multithreading just yet.
 
Berty Ahern
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats true!
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, here's a lecture on statics. A static member (variable, method, whatever) is a member which can be accessed by using the class name as opposed to invoking them upon an instance of that class. Take the methods in the java.lang.Math class for example. They're all static. You invoke them like this...



You don't instantiate (in fact you can't because the constructor is private) an instance of Math and call the methods on the instance like this...



So, in a static method (such as main), you don't have access to non-static methods (unless you call them on an instance of that class). In your case, you're using an anonymous inner class, but you still don't have an instance on which to call the method, so you should just make it static...

 
Berty Ahern
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OMG it worked. I just put in the word static and it works!
Thanks again man!
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem! And, I typed all that for nothing. I could have just said "put static in front of void and it'll work."
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

No problem! And, I typed all that for nothing. I could have just said "put static in front of void and it'll work."


but others might read and learn from your post
btw, Color is the same with its static methods
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But, you can instantiate Color.
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by James Carman:
I typed all that for nothing.

OMG, I've been using "static" incorrectly for ten years! Thanks for correcting me, James!

Do you feel better now?
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by David Harkness:
OMG, I've been using "static" incorrectly for ten years! Thanks for correcting me, James!

Do you feel better now?



Coming from you, I'm not so sure that's sincere. But, I'll chug one with ya anyway!
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And here, all these years I thought it had something to do with electricity...
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic