Results 1 to 4 of 4

Thread: static methods

  1. #1
    billq is offline Member
    Join Date
    Dec 2009
    Posts
    25
    Rep Power
    0

    Default static methods

    Hello, I made a simple console program which has a main method and another method I'll call testMethod. If I call the method from inside main I get a compiler error about the testMethod not being static. If I make the method static all is well. I did not create the executing program using the new command but I would believe the object for the class is created when the program executes.

    Please alleviate my confusion.
    thanks
    bill

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You are best to read Sun's Java tutorial from the start. An instance of your class is created if you used the new keyword to create it. You can then call the instance methods on that instance. If you didn't create an instance in your code, only the class is loaded and that's why you can call the static methods (class methods).

  3. #3
    gcampton Guest

    Default

    what r0*x means is, if you try to call methods in other classes or even the same class as:

    SomeMethod();

    then those methods must be static, however nonstatic methods or also known as class member functions need to be created instances of them. eg.

    Java Code:
    public class StaticVsNot
    {
        public static void main(String[] args) 
        {
            //first call to static method no instance is created.
        	staticWelcome();
        	
            // create an instance of the class to call it's member functions.
        	StaticVsNot svn = new StaticVsNot();
        	svn.nonStaticWelcome();
        }
        
        public static void staticWelcome()
        {
        	System.out.println("Static welcome");
        }
        
        public void nonStaticWelcome()
        {
        	System.out.println("normal welcome");
        }
    }
    There are various advantages and disadvantages to using static methods, as you will read in the tutorials.
    You will have already used this using the Scanner class for reading input. Scanner classes methods are not static which is why you need to create an instance and to use dot notation.
    Last edited by gcampton; 12-30-2009 at 03:35 PM.

  4. #4
    billq is offline Member
    Join Date
    Dec 2009
    Posts
    25
    Rep Power
    0

Similar Threads

  1. Replies: 8
    Last Post: 04-17-2012, 12:00 AM
  2. Recursion with static and non static methods
    By sh4dyPT in forum New To Java
    Replies: 14
    Last Post: 03-27-2009, 07:56 AM
  3. Static methods
    By carderne in forum New To Java
    Replies: 10
    Last Post: 01-03-2009, 06:27 PM
  4. Static methods
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-04-2007, 06:56 PM

Posting Permissions

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