Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Load on startup

 
Berty Ahern
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
How did you make the main method static?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Thats true!
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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."
 
miguel lisboa
Ranch Hand
Posts: 1281
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
But, you can instantiate Color.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • 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
  • 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!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And here, all these years I thought it had something to do with electricity...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic