Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes non-static method isSenior() cannot be referenced from a static context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "non-static method isSenior() cannot be referenced from a static context" Watch "non-static method isSenior() cannot be referenced from a static context" New topic
Author

non-static method isSenior() cannot be referenced from a static context

Steve Starr
Greenhorn

Joined: Apr 09, 2003
Posts: 1
I am calling a non-static instance method to a static context. How can i get around this? i know it is probably a simple problem, but please help. its a long source file so i have a text file link Source File. Thank You very much
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
You have two choices:
  • Declare the method static.
  • Create an instance and call the method thru the instance.


  • Most likely you'll want to do the second since getter methods like isSenior() are generally used to retrieve state from a particular instance.
    I noticed a call Senior.isSenior() in your class, but saw neither a reference nor a class named Senior.


    Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
    sunil shrestha
    Greenhorn

    Joined: Apr 17, 2003
    Posts: 1
    Steve,
    I agree with Mike, create an instance and call the method. Static methods should be reserved for CLASS functionality. The 'isSenior' method is really associated with an INSTANCE of Student. Also making 'isSenior' static would mean that it could only access static class variables and you have a reference to the instance variable 'creditsEarned'.
    Understand that static variables and methods are created at class load time, ie before any instance is created when the non static members are created. If you never instantiate a Student object, the class will still have a reference to all static members but not to any of the non-static ones.
    Sunil
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    It looks like you actually meant to do this:

    I also have a suggestion to help with managing your code. I noticed that main() is very long. You may try to break individual tasks into separate functions. Mostly this helps isolate ideas, so that you don't suffer from data overload (trying to remember too many things at the same time). It would also help when you ask questions here as you should be able to post a single function that has problems. For the most part, these individual functions can be self-contained, especially in this case when they would most likely all be static.


    Java API Documentation
    The Java Tutorial
     
    GeeCON Prague 2014
     
    subject: non-static method isSenior() cannot be referenced from a static context