Difference between static and instance method

Java Predefined & User defined Methods, Java String Methods, Java Number Methods, Java Array Methods and Java Character Methods. Java User defined methods with return a value, User defined methods without return any value, and Java Static & Non-static Methods.
Post Reply
AyubHussain
Posts: 142
Joined: Fri Nov 23, 2018 2:48 am

Difference between static and instance method

Post by AyubHussain » Thu Nov 29, 2018 10:22 am

What is the difference between static (class) method and instance method?

ramya
Posts: 89
Joined: Fri Nov 23, 2018 2:55 am

Re: Difference between static and instance method

Post by ramya » Thu Nov 29, 2018 10:25 am

static or class method :

1)A method that is declared as static is known as the static method.

2)We don't need to create the objects to call the static methods.

3)Non-static (instance) members cannot be accessed in the static context (static method, static block, and static nested class) directly.

4)For example: public static int cube(int n){ return n*n*n;}

instance method :

1)A method that is not declared as static is known as the instance method.

2)The object is required to call the instance methods.

3)Static and non-static variables both can be accessed in instance methods.

4)For example: public void msg(){...}.

sekhar
Posts: 42
Joined: Mon Dec 31, 2018 6:40 am

Re: Difference between static and instance method

Post by sekhar » Sat Jan 05, 2019 11:16 am

Instance Method:
==============
Instance method are methods which require an object of its class to be created before it can be called. To invoke a instance method, we have to create an Object of the class in within which it defined.
These methods themselves are stored in Permanent Generation space of heap but the parameters (arguments passed to them) and their local variables and the value to be returned are allocated in stack. They can be called within the same class in which they reside or from the different classes defined either in the same package or other packages depend on the access type provided to the desired instance method.
Instance method(s) belong to the Object of the class not to the class i.e. they can be called after creating the Object of the class.
Every individual Object created from the class has its own copy of the instance method(s) of that class.
They can be overridden since they are resolved using dynamic binding at run time.

Static Method:
============
Static methods are the methods in Java that can be called without creating an object of class. They are referenced by the class name itself or reference to the Object of that class.
They are stored in Permanent Generation space of heap as they are associated to the class in which they reside not to the objects of that class. But their local variables and the passed argument(s) to them are stored in the stack. Since they belong to the class so they can be called to without creating the object of the class.
Static method(s) are associated to the class in which they reside i.e. they can be called even without creating an instance of the class i.e ClassName.methodName(args).
They are designed with aim to be shared among all Objects created from the same class.
Static methods can not be overridden. But can be overloaded since they are resolved using static binding by compiler at compile time.

srini
Posts: 76
Joined: Thu Jan 03, 2019 3:58 am

Re: Difference between static and instance method

Post by srini » Thu Jan 10, 2019 4:09 am

Difference between Static methods and Instance methods:
=======================================================
Instance method are methods which require an object of its class to be created before it can be called. Static methods are the methods in Java that can be called without creating an object of class.
Static method is declared with static keyword. Instance method is not with static keyword.
Static method means which will exist as a single copy for a class. But instance methods exist as multiple copies depending on the number of instances created for that class.
Static methods can be invoked by using class reference. Instance or non static methods are invoked by using object reference.
Static methods can’t access instance methods and instance variables directly. Instance method can access static variables and static methods directly.

Post Reply

Return to “Java Methods”