• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Steve Starr
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    sunil shrestha
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 3061
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic