The difference between static methods and instance methods

I just read the text given to me in my textbook, and I'm not quite sure that I understand what he is saying. This basically tells me that static or class methods include the static modifier keyword. But I don't know what that means?

Can someone please explain to me in real simple terms what static or cool methods are?

Also, can I get a simple explanation of which instance methods?

This is what they give me in the textbook:

There are important practical consequences of the presence or absence of a static modifier. The public class method can be called and executed as soon as Java processes the definition of the class to which it belongs. This does not apply to the instance method. Before calling and executing the public instance method, an instance of the class to which it belongs must be created. To use the open class method, you just need a class. On the other hand, before you can use the open instance method, you must have an instance of the class.

The way that a static method is called in the definition of another method depends on whether both methods belong to the same class or not. In the above example, factorial and main are both methods of the MainClass class. As a result, the factorial call in the definition of main simply refers to the name of the method, factorial.

+47
java static static-methods non-static
Aug 16 '12 at 18:15
source share
9 answers

The basic paradigm in Java is that you write classes and create these classes. Created objects (class instance) have attributes associated with them (member variables) that affect their behavior; when an instance has executed its method, it will refer to these variables.

However, all objects of a certain type may have behavior that does not depend on member variables at all; these methods are best static. Statically, an instance of the class is not required to run the method.

You can do this to execute a static method:

MyObject.staticMethod();//Simply refers to the class static code 

But in order to execute the non-static method you have to do this:

 MyObject obj = new MyObject();//Create an instance obj.nonstaticMethod();//Refer to the instance class code 

At a deeper level, when the compiler puts a class together, it contains several pointers to methods. When these methods are executed, it follows the pointers and executes the code at the far end. If an instance of the class is created, the created object contains a pointer to a "virtual method table" that points to the methods that should be called for this particular class in the inheritance hierarchy. However, if the method is static, a β€œvirtual method table” is not required: all calls to this method go to the same place in memory to execute the same code. For this reason, in high-performance systems, it is better to use the static method if you are not dependent on instance variables.

+87
Aug 16 '12 at 18:18
source share
β€” -

Methods and variables that are not declared static are known as instance methods and instance variables. To refer to instance methods and variables, you must first create an instance of the class, in order to first create an object of this class. For statics, you do not need to create an instance of class u, you can access methods and variables with the class name using the period sign that is in (.)

, eg:

 Person.staticMethod(); //accessing static method. 

for a non-static method, you must create an instance of the class.

 Person person1 = new Person(); //instantiating person1.nonStaticMethod(); //accessing non-static method. 
+11
Jul 03 '15 at 11:24
source share

Static methods, variables apply to the whole class, not just an instance of an object. A static method, a variable is associated with the class as a whole, and not with specific instances of the class. Each object will have a common copy of static methods, variables. There is only one copy for each class, regardless of how many objects are created from it.

+6
Aug 16 '12 at 18:24
source share

Instance Methods => called on a specific instance of a particular class. The method wants to know in which class it was called. How this happens is an invisible parameter called 'this'. Inside 'this', we have instance class members already set with values. 'this' is not a variable. This value, you cannot change it, and this value refers to the recipient of the call. Example: You call repairmen (instance method) to fix your TV (actual program). It comes with tools (the 'this' parameter). It comes with certain tools needed to install the TV, and it can also fix other things.

In static methods => there is no such thing as 'this'. Example: The same repairman (static method). When you call him, you must indicate which repairman should call (for example, an electrician). And he will come and fix your TV. But it has no tools to fix other things (the 'this' parameter is missing).

Static methods are usually useful for operations that do not require any data from an instance of the class (from 'this') and can fulfill their purpose exclusively using their arguments.

+2
Aug 22 '16 at 15:35
source share

The difference between static methods and instance methods

  • An instance method is methods that require the creation of an object of its class before it is called. Static methods are methods in Java that can be called without creating a class object.

  • The static method is declared with the static keyword. The instance method is not with a static keyword.

  • The static method means that it will exist as a single copy for the class. But instance methods exist as multiple copies, depending on the number of instances created for this class.

  • Static methods can be called using class references. Instances or non-static methods are invoked using an object reference.

  • Static methods directly affect instance methods and instance variables. The Instance method can directly access static variables and static methods.

Link: geeksforgeeks

+2
Apr 02 '17 at 18:17
source share

The behavior of an object depends on the variables and methods of this class. When we create a class, we create an object for it. For static methods, we do not require them, since static methods mean that all objects will have the same copy, so there is no need for an object. eg:

 Myclass.get(); 

In the case of a method, each object will have a different behavior, so they need to call the method using an instance of the object. eg:

 Myclass x = new Myclass(); x.get(); 
+1
Apr 19 '16 at 5:35
source share

If the state of the method should not be changed or it will not use any instance variables.

You want to call a method without an instance.

If it only works with the provided arguments.

Useful functions are a good example of static methods. ie math.pow (), this function will not change state for different values. This is static.

+1
Aug 16 '16 at 0:20
source share

The static modifier in front of the function implies that there is only one copy of this function. If the static modifier is not placed in front of the function, then with each object or instance of this class a new copy of this function is created. :) The same thing happens with variables.

0
Aug 16 '12 at 18:20
source share

In short, static methods and static variables are class levels, where, since instance methods and instance variables are instance or object level.

This means that whenever an instance or object is created (using the new ClassName ()), this object will save its own copy of instace variables. If you have five different objects of the same class, you will have five different copies of instance variables. But static variables and methods will be the same for all these five objects. If you need something in common for each created object, make it static. If you need a method that does not require processing of specific objects, make it static. The static method will only work with a static variable or will return data based on the arguments passed.

 class A { int a; int b; public void setParameters(int a, int b){ this.a = a; this.b = b; } public int add(){ return this.a + this.b; } public static returnSum(int s1, int s2){ return (s1 + s2); } } 

In the above example, when you call add () like:

 A objA = new A(); objA.setParameters(1,2); //since it is instance method, call it using object objA.add(); // returns 3 B objB = new B(); objB.setParameters(3,2); objB.add(); // returns 5 //calling static method // since it is a class level method, you can call it using class itself A.returnSum(4,6); //returns 10 class B{ int s=8; int t = 8; public addition(int s,int t){ A.returnSum(s,t);//returns 16 } } 

In the first class, add () will return the sum of the data passed by a specific object. But the static method can be used to get the sum from any class that is not independent, if any particular instance or object. Consequently, for general methods that require only arguments to work, you can make them static in order to keep everything DRY.

0
Jun 22 '17 at 4:27
source share



All Articles