• Post Reply Bookmark Topic Watch Topic
  • New Topic

method declaration  RSS feed

 
chris barr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i get an error in my code saying that i can't "non-static method exportplayerleveltoOtherClass() cannot be referenced from a staic context" ... i forget what static in "public static exportplaerleveltoOtherclass" means ...
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means it belongs to the class. A non-static or instance method must be invoked on a class instance, it cannot simply be invoked on the class. So if you have a static method it cannot invoke a non-static method without supplying an instance for it to be invoked on.



The first one simply invokes "fooBar()". It's essentially like trying to invoke fooBar on the class itself instead of an instance of the class. Since fooBar is an instance method you can't do this. In the second example we create a new instance of Foo with the new keyword and then invoke fooBar on that instance, which is valid. In the last example we declare the reference "obj" and assign to it a reference to an instance of Foo created with the new keyword. Then we invoke fooBar on that reference, which is also valid since it is an instance of Foo.
 
chris barr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
chris barr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator





exportplayerleveltoOtherClass() is in my login class while my xp method is in my xpstuff class niether one is static ... and i tryed seting it to an instance variable first... i am a little lost here... also i don't know if this belongs in the beginner board.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by chris barr:





exportplayerleveltoOtherClass() is in my login class while my xp method is in my xpstuff class niether one is static ... and i tryed seting it to an instance variable first... i am a little lost here... also i don't know if this belongs in the beginner board.


exportplayerleveltoOtherClass is a method, you're trying to instantiate it like a class. Try
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As an aside, you can name your methods whatever you want, but it would save you a lot of typing, and the method name would be a lot more descriptive, if you came up with a shorter name for this method like
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, please read this:

http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

While I'm not somebody that believes we all have to abide by strict coding conventions at least following the commonly practiced and used ones accepted by the vast majority of the community is appreciated. It will make your code much easier to read and understand and consequently make it easier for us to help you.

Edit: Since that's a rather long document I would refer you to chapters 7 and 9 as probably being the most important and influential to making your code easily readable to us here.
[ May 05, 2006: Message edited by: Ken Blair ]
 
chris barr
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks all
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!