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: What's the difference between a static and non-static method?

  1. #1
    Member
    Join Date
    Feb 2012
    Posts
    96
    Thanks
    15
    Thanked 1 Time in 1 Post

    Default What's the difference between a static and non-static method?

    I always used static without knowing exactly why.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: What's the difference between a static and non-static method?

    Did you try to do a web search? This is the first hit for me, and probably a resource for many of your questions now or in the future
    Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    28
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's the difference between a static and non-static method?

    If you want a particular method of a class (w.r.t its access specifiers) to be called, without creating an object of that class we usually make it as static.
    But on the other hand if you want a method of a class to be called only when its instance/object is created then we make it non static.[i.e we don't suffix it with the keyword static,which in turn by default become non static method].

    Basic example is your public static void main(string args[]){} (start point method)of any application.

  4. #4
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: What's the difference between a static and non-static method?

    Quote Originally Posted by rohan22 View Post
    If you want a particular method of a class (w.r.t its access specifiers) to be called, without creating an object of that class we usually make it as static.
    But on the other hand if you want a method of a class to be called only when its instance/object is created then we make it non static.[i.e we don't suffix it with the keyword static,which in turn by default become non static method].

    Basic example is your public static void main(string args[]){} (start point method)of any application.
    I too have learned to make the compiler happy with out truly understanding what static means.
    So does this make sense then?
    public class Creator {
     
    	//visual of default constructor
    	public Creator(){}
     
    	//empty methods 1st non static, 2nd static.
    	public void nonStaticMethod(){}
     
    	public static void staticMethod(){}
     
    	//main method
    	public static void main(String[] args){
    		Creator methodUser = new Creator();
     
    		methodUser.nonStaticMethod();
    		methodUser.staticMethod();
    		//Creator.nonStaticMethod(); // ***doesnt compile***
    		Creator.staticMethod();
    	}
    }

    So to be clear, can someone explain these 4 situations separately?

    1)methodUser.nonStaticMethod();

    2)methodUser.staticMethod();

    3)Creator.nonStaticMethod(); // ***doesnt compile***

    4)Creator.staticMethod();

    Feel free to add any deeper understanding to fully reveal what static means.

    Thanks!
    Jonathan.

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Posts
    28
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's the difference between a static and non-static method?

    So to be clear, can someone explain these 4 situations separately?

    1)methodUser.nonStaticMethod();

    2)methodUser.staticMethod();

    3)Creator.nonStaticMethod(); // ***doesnt compile***

    4)Creator.staticMethod();
    As i said here is certain changes to your queries

    1)methodUser.nonStaticMethod();.............. works fine.

    2)methodUser.staticMethod();...............you can simply call as staticMethod();....its of the same class.

    3)Creator.nonStaticMethod(); // ***doesnt compile***...........will not compile because you are calling non static method
    using its class name and not using the class's instance/object...i.e methodUser. To call a non static method of any class you
    need to create an instance/object of that class.

    4)Creator.staticMethod();..........call it simply as staticMethod(); since its of the same class.But yes if its of different class say
    Creator2{
    staticMethod();
    }
    then you can call it from Creator class as Creator2.staticMethod(); (Assuming these classes are in the same package or imported)

Similar Threads

  1. Replies: 9
    Last Post: November 5th, 2011, 10:22 AM
  2. Troubles with toString and static and non-static
    By BadgerWatch in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 3rd, 2011, 05:04 AM
  3. [SOLVED] non static variable this cant be referenced from a static context
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 20th, 2011, 06:13 PM
  4. non-static method cannot be referenced from a static context
    By Kaltonse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2010, 07:51 PM
  5. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM