Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Non static method error

 
shawn sandy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic