aspose file tools*
The moose likes Beginning Java and the fly likes Load on startup Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Load on startup" Watch "Load on startup" New topic
Author

Load on startup

Berty Ahern
Greenhorn

Joined: Mar 03, 2005
Posts: 17
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

Joined: Feb 20, 2001
Posts: 580
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.


James Carman, President<br />Carman Consulting, Inc.
Berty Ahern
Greenhorn

Joined: Mar 03, 2005
Posts: 17
Eh whats static?
it's non static. How should I change it to static?
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
How did you make the main method static?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Berty Ahern
Greenhorn

Joined: Mar 03, 2005
Posts: 17
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

Joined: Feb 20, 2001
Posts: 580
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

Joined: Mar 03, 2005
Posts: 17
Thats true!
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
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

Joined: Mar 03, 2005
Posts: 17
OMG it worked. I just put in the word static and it works!
Thanks again man!
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
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

Joined: Feb 08, 2004
Posts: 1281
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


java amateur
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
But, you can instantiate Color.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
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

Joined: Feb 20, 2001
Posts: 580
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

Joined: Jan 30, 2000
Posts: 18671
And here, all these years I thought it had something to do with electricity...


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Load on startup