This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Non static method error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Non static method error" Watch "Non static method error" New topic
Author

Non static method error

shawn sandy
Greenhorn

Joined: Jul 02, 2001
Posts: 12
could anyone explain why I get this error, and how to fix it?
test.java:15: non-static method objectWrite(Golfer) cannot be referenced from a static context
here are the files...

Thanks in advance!
edited by Dave to put in code blocks
[ February 07, 2002: Message edited by: Dave Vick ]
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Shawn
Your method objectWrite is defined as an instance method but in main you are calling it as if it were a static method - you didn't give it a this:
calling it in main like this will work:
golfer.objectWrite(golfer);
of course that is redundant to tell it the same object twice.
It looks like you just want a method that will save the given golfer to the file system. You can make the method static:
static public void objectWrite(Golfer player){} then it'll act the way you are using it or you could just make it an instance method of class Golfer and not have it take an arguement it would just work on the this object it gets in the method call. I'mmnot real proficient with serialization so I cant tell you the best implementation but I can show you whats wrong with you've got
hope that helps


Dave
 
Don't get me started about those stupid light bulbs.
 
subject: Non static method error
 
Similar Threads
java.lang.reflect.UndeclaredThrowableException
Displaying ArrayList data in a JTable
Related to Object Serialization
Serialization
how FileOutputStream and ObjectOutputStream working here