What is the difference between a static method and a non-static method?

See code snippets below:

Code 1

public class A { static int add(int i, int j) { return(i + j); } } public class B extends A { public static void main(String args[]) { short s = 9; System.out.println(add(s, 6)); } } 

Code 2

 public class A { int add(int i, int j) { return(i + j); } } public class B extends A { public static void main(String args[]) { A a = new A(); short s = 9; System.out.println(a.add(s, 6)); } } 

What is the difference between these code snippets? Both outputs 15 as an answer.

+57
java
Oct 11 2018-10-10T00:
source share
14 answers

The static method belongs to the class itself, and the non-static method (aka instance) belongs to every object that is generated from this class. If your method does something that does not depend on the individual characteristics of its class, make it static (this will reduce the size of the program). Otherwise, it must be non-static.

Example:

 class Foo { int i; public Foo(int i) { this.i = i; } public static String method1() { return "An example string that doesn't depend on i (an instance variable)"; } public int method2() { return this.i + 1; // Depends on i } } 

You can call static methods as follows: Foo.method1() . If you try this with method2, it will not work. But this will work: Foo bar = new Foo(1); bar.method2(); Foo bar = new Foo(1); bar.method2();

+124
Oct 11 '10 at 4:41
source share

Static methods are useful if you have only one instance (situation, circumstance) in which you intend to use this method, and you do not need several copies (objects). For example, if you write a method that registers with one and only one website, downloads weather data, and then returns values, you can write it as static, because you can hard-code all the necessary data in the method and you wonโ€™t have multiple copies or copies. You can then access the method statically using one of the following:

 MyClass.myMethod(); this.myMethod(); myMethod(); 

Non-static methods are used if you intend to use your method to create multiple copies. For example, if you want to download meteorological data from Boston, Miami and Los Angeles, and if you can do this from your method without individually setting the code for each individual location, then you access the method non-statically

 MyClass boston = new MyClassConstructor(); boston.myMethod("bostonURL"); MyClass miami = new MyClassConstructor(); miami.myMethod("miamiURL"); MyClass losAngeles = new MyClassConstructor(); losAngeles.myMethod("losAngelesURL"); 

In the above example, Java creates three separate objects and memory cells from the same method, which you can get individually using the "boston", "miami" or "losAngeles" link. You cannot access any of the above statically, because MyClass.myMethod (); It is a general reference to a method, and not to individual objects created by a non-static link.

If you are faced with a situation where the method of accessing each location or the method of returning data is quite different, you cannot write the โ€œone size fits allโ€ method without jumping over many hoops, you can better accomplish your task by writing three separate static methods: one for each location.

+24
Jul 06 '13 at 20:40
source share

Here's an understanding of instances and class members , this is explained very well.

+10
Oct 11 2018-10-10T00:
source share

Usually

static : there is no need to create an object that we can directly call with

 ClassName.methodname() 

Non Static : we need to create an object like

 ClassName obj=new ClassName() obj.methodname(); 
+8
05 Sep '14 at 5:19
source share

The static method belongs to the class and the non-static method belongs to the class object. This non-static method can be called on an object of the class that it belongs to. The static method may however be called either a class or a class object. The static method can only access static members. A non-static method can access both static and non-static members, because at the time the static method, the class may not be an instance (if it is a call to the class itself). in another case, a non-static method can only be called when the class has already been created. The static method is shared by all instances of the class. These are some of the main differences. I would also like to point out the often ignored difference in this context. Whenever a method called in C ++ / Java / C #, an implicit argument (link 'this') passed along with / without other parameters. In the case of a static method call, the 'this' link is not passed as static methods, belong to the class and, therefore, do not have a 'this' link.

Reference : Static Vs non-static methods

+6
Oct 11 '10 at 4:40
source share

Well, more technically speaking, the difference between the static method and the virtual method is related to how they are related.

The traditional "static" method, as in most non-OO languages, is bound / connected "statically" to its implementation at compile time. That is, if you call the Y () method in program A and link your program A to the library X that implements Y (), the address XY () is rigidly bound to A, and you cannot change that.

In OO languages โ€‹โ€‹such as JAVA, "virtual" methods are resolved "late" at run time, and you need to provide an instance of the class. So, in program A, to call the virtual method Y (), you need to provide an instance of BY (), for example. At run time, every time A calls BY (), the called implementation will depend on the instance used, therefore BY (), CY (), etc. They can potentially provide different implementations of Y () at runtime.

Why do you need this? Because you can separate your code from dependencies. For example, let's say program A executes "draw ()". With a static language, that is, but with OO you will do B.draw (), and the actual drawing will depend on the type of object B, which at runtime can change the square of the circle, etc. This way, your code can draw a few things without having to change it, even if new types B appear AFTER the code is written. Nifty -

+5
Sep 27 '13 at 18:00
source share

The static method belongs to the class, and the non-static method belongs to the class object. I gave one example of how it makes the difference between outputs.

 public class DifferenceBetweenStaticAndNonStatic { static int count = 0; private int count1 = 0; public DifferenceBetweenStaticAndNonStatic(){ count1 = count1+1; } public int getCount1() { return count1; } public void setCount1(int count1) { this.count1 = count1; } public static int countStaticPosition() { count = count+1; return count; /* * one can not use non static variables in static method.so if we will * return count1 it will give compilation error. return count1; */ } } public class StaticNonStaticCheck { public static void main(String[] args){ for(int i=0;i<4;i++) { DifferenceBetweenStaticAndNonStatic p =new DifferenceBetweenStaticAndNonStatic(); System.out.println("static count position is " +DifferenceBetweenStaticAndNonStatic.count); System.out.println("static count position is " +p.getCount1()); System.out.println("static count position is " +DifferenceBetweenStaticAndNonStatic.countStaticPosition()); System.out.println("next case: "); System.out.println(" "); } } 

}

Now the output will be:

 static count position is 0 static count position is 1 static count position is 1 next case: static count position is 1 static count position is 1 static count position is 2 next case: static count position is 2 static count position is 1 static count position is 3 next case: 
+3
Sep 19 '12 at 12:11
source share

If your method is related to the characteristics of an object, you should define it as a non-static method. Otherwise, you can define your method as static, and you can use it regardless of the object.

+2
Nov 02 '14 at 19:43
source share

The main difference: non-static members are declared without using the static keyword

All static members (both variables and methods) are passed using the class name. Therefore, static class members are also called class members or class members.

To access the non-stationary members of a class, we must create a reference variable. reference variable stores the object.

+1
Aug 15 '12 at 6:05
source share

Another scenario for a static method.

Yes, the static method refers to the class, not the object. And if you do not want someone to initialize the class object or you do not need more than one object, you need to use a private constructor and, therefore, a static method.

Here we have a private constructor and using the static method we create an object.

Ex ::

 public class Demo { private static Demo obj = null; private Demo() { } public static Demo createObj() { if(obj == null) { obj = new Demo(); } return obj; } } 

Demo obj1 = Demo.createObj ();

There can only be one instance.

+1
Apr 02 '15 at 12:36
source share

Static Method Example

 class StaticDemo { public static void copyArg(String str1, String str2) { str2 = str1; System.out.println("First String arg is: "+str1); System.out.println("Second String arg is: "+str2); } public static void main(String agrs[]) { //StaticDemo.copyArg("XYZ", "ABC"); copyArg("XYZ", "ABC"); } } 

Output:

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

As you can see from the above example, I did not even use an object to call the static method. It can be called directly in the program or using the class name.

Non-static method example

 class Test { public void display() { System.out.println("I'm non-static method"); } public static void main(String agrs[]) { Test obj=new Test(); obj.display(); } } 

Output:

 I'm non-static method 

A non-static method is always called using a class object, as shown in the above example.

Key points:

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 a class object:

 Test obj = new Test(); 
+1
Dec 20 '16 at 7:48
source share

Simply put, from the user's point of view, the static method either does not use any variables at all, or all the variables that it uses are local to the method or are static. Defining a static method provides a slight performance advantage.

+1
Dec 20 '16 at 7:57
source share
 - First we must know that the diff bet static and non static methods is differ from static and non static variables : - this code explain static method - non static method and what is the diff public class MyClass { static { System.out.println("this is static routine ... "); } public static void foo(){ System.out.println("this is static method "); } public void blabla(){ System.out.println("this is non static method "); } public static void main(String[] args) { /* *************************************************************************** * 1- in static method you can implement the method inside its class like : * * you don't have to make an object of this class to implement this method * * MyClass.foo(); // this is correct * * MyClass.blabla(); // this is not correct because any non static * * method you must make an object from the class to access it like this : * * MyClass m = new MyClass(); * * m.blabla(); * * ***************************************************************************/ // access static method without make an object MyClass.foo(); MyClass m = new MyClass(); // access non static method via make object m.blabla(); /* access static method make a warning but the code run ok because you don't have to make an object from MyClass you can easily call it MyClass.foo(); */ m.foo(); } } /* output of the code */ /* this is static routine ... this is static method this is non static method this is static method */ - this code explain static method - non static Variables and what is the diff public class Myclass2 { // you can declare static variable here : // or you can write int callCount = 0; // make the same thing //static int callCount = 0; = int callCount = 0; static int callCount = 0; public void method() { /********************************************************************* Can i declare a static variable inside static member function in Java? - no you can't static int callCount = 0; // error ***********************************************************************/ /* static variable */ callCount++; System.out.println("Calls in method (1) : " + callCount); } public void method2() { int callCount2 = 0 ; /* non static variable */ callCount2++; System.out.println("Calls in method (2) : " + callCount2); } public static void main(String[] args) { Myclass2 m = new Myclass2(); /* method (1) calls */ m.method(); m.method(); m.method(); /* method (2) calls */ m.method2(); m.method2(); m.method2(); } } // output // Calls in method (1) : 1 // Calls in method (1) : 2 // Calls in method (1) : 3 // Calls in method (2) : 1 // Calls in method (2) : 1 // Calls in method (2) : 1 
0
Aug 07 '18 at 17:23
source share

โ€œSometimes you want to have variables that are common to all objects. This is achieved using a static modifier.

i.e. human class - the number of goals (1) is static, the same for all people, however human - the hair color is variable for each person.

Please note that static vars can also be used to exchange information in all instances.

-one
Apr 08 '15 at 12:49
source share



All Articles