Results 1 to 4 of 4

Thread: Static Methods

  1. #1
    nhmllr's Avatar
    nhmllr is offline Member
    Join Date
    Aug 2011
    Posts
    51
    Rep Power
    0

    Default Static Methods

    From what I understand, not static methods can only be used directly by the instance under which the method exists. However, a static method can be accessed with the class name, not the instance name. Tell me if my understanding's wrong.

    Now, I'm working through project euler (really great, check it out if you haven't) and all of the problems are solvable with one class that has a main() in it. However, I like to make methods to organize my thoughts, so I put some methods inside of the class. The compiler would only accept them if they were static, though. Why would that be? Why can't I make methods that are not static in this situation?

    Here's a silly example I just made up:
    Java Code:
    public class example {
    	//main
    	public static void main(String[] args) 
        {
    		int number = 1;
    		System.out.println(triple(number));
        }
    	
    	public static int triple(int n)
    	{
    		return 3*n;
    	}
    }
    If triple() isn't static, it won't compile. Why not? Shouldn't a method always be able to be called from within itself?

    Thanks

  2. #2
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default Re: Static Methods

    It has to be main because you're calling it from the static main method. If you don't want it to be static, call it from the constructor like this:

    Java Code:
    public class example {
    
        public example{
            System.out.println(triple(number));
        }
        //main
        public static void main(String[] args) 
        {
            int number = 1;
            new example();
      
        }
         
        public int triple(int n)
        {
            return 3*n;
        }
    }
    Hope thats helpful.
    Last edited by Guy; 10-03-2011 at 09:42 AM.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,023
    Blog Entries
    7
    Rep Power
    20

    Default Re: Static Methods

    Quote Originally Posted by Guy View Post
    It has to be main because you're calling it from the static main method. If you don't want it to be static, call it from the constructor like this: [ ... ]

    Hope thats helpful.
    No it isn't because your code doesn't even compile. A non-static method can only be called against a target object, i.e. the object that has to execute the body of that method. No object, no method; it's that simple. A static method needs a target, an object somewhere to call a non-static method against. If it hasn't a target object, it can't call that non-static method.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Guy
    Guy is offline Member
    Join Date
    Jul 2011
    Posts
    42
    Rep Power
    0

    Default Re: Static Methods

    My bad, thanks for correcting me!
    I guess I'm still to much of a newbie to be helping :P
    This is what I meant to write though. which does compile:

    public class numbers {

    public numbers() {
    int number = 1;
    System.out.println(triple(number));
    }
    //main
    public static void main(String[] args)
    {
    new numbers();
    }

    public int triple(int n)
    {
    return 3*n;
    }
    }
    Last edited by Guy; 10-03-2011 at 09:58 AM.

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Static and non static class methods question
    By silverglade in forum New To Java
    Replies: 2
    Last Post: 05-14-2011, 10:10 PM
  3. static methods
    By billq in forum New To Java
    Replies: 3
    Last Post: 12-30-2009, 02:34 PM
  4. Recursion with static and non static methods
    By sh4dyPT in forum New To Java
    Replies: 14
    Last Post: 03-27-2009, 06:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •