Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Call without being static? [Question]

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Call without being static? [Question]

    I hope this is the right place to post, if not I'm sorry.

    I'm new to Java and I've been having trouble calling a non-static method.

    I want to call
    public boolean updateSession() {
    			setActivityState(SessionStates.END_STATE);
    			return true;
    		}

    from

    public boolean handle(Player player) {
     
    updateSession();
     
    return true;
     
    }

    but it wants me to make updateSession() static. Is there anything I can do to get around this?
    Last edited by beennn; October 30th, 2012 at 07:33 PM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Call without being static? [Question]

    Consider you have a Person class which has a name attribute and a printName method. If that method was static and you called it which name should it print, mine, yours? There arebillions of people on this planet and you need to know which name you want printed. So you create an instance of the Person class to represent each person on the planet. Then the printName method would be non-static. You can then call the printName method of a specific Person object to get their name. I hope that helps.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Call without being static? [Question]

    I understand what your saying but I don't understand the definitions well enough to solve it, could you post a short example of what you said please?

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Call without being static? [Question]

    Basically you need to create an instance of your class so you can call a non-static method.
    Foo myFoo = new Foo(); // creates an instance of the Foo class
    myFoo.someMethod(); // calls a non-static method
    Foo.anotherMethod(); // calls a static method
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Call without being static? [Question]

    Thank you very much

Similar Threads

  1. non static varible from static context error
    By chopficaro in forum What's Wrong With My Code?
    Replies: 12
    Last Post: June 5th, 2012, 07:07 PM
  2. Replies: 1
    Last Post: April 3rd, 2012, 06:32 AM
  3. update static field and call method within the class?
    By GeneralPihota in forum Object Oriented Programming
    Replies: 7
    Last Post: February 6th, 2012, 09:20 PM
  4. Question Dynamically create objects, set value and call
    By buntyindia in forum Java Theory & Questions
    Replies: 1
    Last Post: May 25th, 2011, 07:50 AM
  5. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM