beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

The difference between regular (non-static) and static methods

By Chaitanya Singh | Filed Under: OOPs Concept

Java is a Object Oriented Programming(OOP) language, which means we need objects to access methods and variables inside of a class. However this is not always true. While discussing static keyword in java, we learned that static members are class level and can be accessed directly without any instance. In this article we will see the difference between static and non-static methods.

Static Method Example

class StaticDemo
{
   public static void copyArg(String str1, String str2)
   {
       //copies argument 2 to arg1
       str2 = str1;
       System.out.println("First String arg is: "+str1);
       System.out.println("Second String arg is: "+str2);
   }
   public static void main(String agrs[])
   {
      /* This statement can also be written like this: 
       * StaticDemo.copyArg("XYZ", "ABC");
       */
      copyArg("XYZ", "ABC");
   }
}

Output:

First String arg is: XYZ
Second String arg is: XYZ

As you can see in the above example that for calling static method, I didn’t use any object. It can be accessed directly or by using class name as mentioned in the comments.

Non-static method example

class JavaExample
{
   public void display()
   {
       System.out.println("non-static method");
   }
   public static void main(String agrs[])
   {
	   JavaExample obj=new JavaExample();
	   /* If you try to access it directly like this:
	    * display() then you will get compilation error
	    */
       obj.display();
   }
}

Output:

non-static method

A non-static method is always accessed using the object of class as shown in the above example.

Notes:
How to call static methods: direct or using class name:

StaticDemo.copyArg(s1, s2);
OR copyArg(s1, s2);

How to call a non-static method: using object of the class:

JavaExample obj = new JavaExample();

Important Points:

  1. Static Methods can access static variables without any objects, however non-static methods and non-static variables can only be accessed using objects.
  2. Static methods can be accessed directly in static and non-static methods. For example the static public static void main() method can access the other static methods directly. Also a non-static regular method can access static methods directly.

Comments

  1. Prasannaraj says

    December 16, 2014 at 8:03 AM

    Hi,

    Why can’t we use in last example?
    public static void main(String args[])
    {
    System.out.println(“Age is:”+ getAge(15));
    }

    Reply
    • praveen says

      December 26, 2014 at 5:44 AM

      Because to access any non static members we have to first create object of that class(Here Sample is class), and then access the methods.
      Now,
      Public static void main()
      {
      Sample obj= new Sample();
      Obj.setAge(30);
      System.output.println(“Age=”+obj.getAge());
      }

      Reply
  2. Poonam Marathe says

    January 19, 2015 at 5:39 PM

    where the static variables are get stored?and how they get processed?

    Reply
    • gaensh chowkekar says

      February 22, 2017 at 3:51 AM

      static variable are stored in class common memory

      Reply
    • meghamsh says

      May 4, 2017 at 1:30 PM

      The Static variables are stored in heap memory….basically we use static to define a fixed final variable because it stays constant through out the application…
      e.g. private static final float pi=3.14f;

      Reply
  3. akintunde says

    June 10, 2015 at 1:17 PM

    good day, please can you explain why the error when the age isn’t assigned to an object?

    Reply
    • laxman says

      January 30, 2016 at 2:41 AM

      like said earlier,it is because for the non static method to be used in static, an object should be created first and then called using that obj eg. obj.getage() in static method.
      hope it helps

      Reply
  4. Abhay jadhav says

    August 4, 2015 at 1:28 PM

    Nice stuff related to java
    Thank you it really helps

    Reply
  5. Haider Ali says

    March 8, 2017 at 4:46 PM

    println is a static funtion or non- static?
    plz discuss in detail.

    Reply
  6. Vidhisha says

    May 6, 2017 at 8:12 AM

    Hi,

    Can you please give example of static methods in terms of real time application.Where ,how and what is the purpose of using static methods.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

  • Java Index
  • Java Introduction
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Control Statements

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor
  • Java Interview Q

MORE ...

  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap