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 2 of 2

Thread: Is there a difference between calling a static method

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    22
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Is there a difference between calling a static method

    private static void staticMethod() {
    		System.out.println("static method");
    	}

    Is there a difference between calling a static method like:

    without class name
    staticMethod();

    Vs:

    with class name
    demo.staticMethod();

    Vs
    demo obj = null;
    obj.staticMethod();


    Vs
    demo obj = new demo();
    obj.staticMethod();

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Is there a difference between calling a static method

    The code should use the class's name when calling a static method.
    ClassName.method();

    Do not use a reference to an instance of the class to call a static method.

    Do some reading in the Java tutorial: http://docs.oracle.com/javase/tutori...ybigindex.html
    https://docs.oracle.com/javase/tutorial/index.html
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    jenniferruurs (October 7th, 2019)

Similar Threads

  1. Threads calling static and non-static method
    By dhasija in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 14th, 2014, 06:05 AM
  2. Calling A Public Static Void Method from Main
    By Clubs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 27th, 2013, 02:29 AM
  3. [SOLVED] Calling a non-static method outside the package without using extends
    By iamhealed in forum Java Theory & Questions
    Replies: 6
    Last Post: September 8th, 2012, 11:04 PM
  4. What's the difference between a static and non-static method?
    By wholegrain in forum Java Theory & Questions
    Replies: 4
    Last Post: February 23rd, 2012, 01:06 AM
  5. Replies: 1
    Last Post: February 14th, 2012, 07:42 AM