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:
- Static Methods can access static variables without any objects, however non-static methods and non-static variables can only be accessed using objects.
- 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.
Prasannaraj says
Hi,
Why can’t we use in last example?
public static void main(String args[])
{
System.out.println(“Age is:”+ getAge(15));
}
praveen says
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());
}
Poonam Marathe says
where the static variables are get stored?and how they get processed?
gaensh chowkekar says
static variable are stored in class common memory
meghamsh says
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;
akintunde says
good day, please can you explain why the error when the age isn’t assigned to an object?
laxman says
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
Abhay jadhav says
Nice stuff related to java
Thank you it really helps
Haider Ali says
println is a static funtion or non- static?
plz discuss in detail.
Vidhisha says
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.