Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting static method error- but it's not static!

Status
Not open for further replies.

MissouriTiger

Programmer
Oct 10, 2000
185
US
It's me again, with another compiler mystery...

The method below is generating the following compiler error message, but as you can see it's NOT a static method. Any suggestions??

non-static method getUser(java.lang.String) cannot be referenced from a static context

 
You are trying to call the method without first instantiating the object. For Example:
Wrong Way:
Code:
Object.toString(); // Will return same compiler error
Correct Way:
Code:
Object obj = new Object();
obj.toString(); // Works fine
Solution:
Either create your object first or if the method needs to be "globally" accessible then make it static. Remember static methods may only access static instance variables. Wushutwist
 
Wushutwist, you are half right. What he did was access a non-static method from a static method, which you can't do so.

What you can do is either change all your methods to static or to non-static. This case, it wouldn't make you confuse so easily. Remember static methods can ONLY access static variables/methods. Non-static methods can access static variables/methods AND non-static variables/methods.

Regards,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Actually, I was not trying to access a non-static method from a static method. The method was NOT static, which was the source of my confusion. I meant to paste in my code so you could see that, but I neglected to do so.

I don't even remember what happened now, except that my code had a few errors which were confusing the compiler, but the keyword static was not in my code.

Thanks for the help guys.
 
I'm going to make the guess that you were trying to access a non-static method from main(). Main is static so this would be a no-no. Wushutwist
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top