Difference between Static methods and Instance methods
up vote
62
down vote
favorite
I was just reading over the text given to me in my textbook and I'm not really sure I understand what it is saying. It's basically telling me that static methods or class methods include the "modifier" keyword static. But I don't really know what that means?
Could someone please explain to me in really simple terms what Static or Class Methods are?
Also, could I get a simple explanation on what Instance methods are?
This is what they give me in the textbook:
There are important practical implications of the presence or absence of the static modifier. A public class method may be invoked and executed as soon as Java processes the definition of the class to which it belongs. That is not the case for an instance method. Before a public instance method may be invoked and executed, an instance must be created of the class to which it belongs. To use a public class method, you just need the class. On the other hand, before you can use a public instance method you must have an instance of the class.
The manner in which a static method is invoked within the definition of another method varies according to whether or not the two methods belong to the same class. In the example above, factorial and main are both methods of the MainClass class. As a result, the invocation of factorial in the definition of main simply references the method name, "factorial".
java static static-methods non-static
add a comment |
up vote
62
down vote
favorite
I was just reading over the text given to me in my textbook and I'm not really sure I understand what it is saying. It's basically telling me that static methods or class methods include the "modifier" keyword static. But I don't really know what that means?
Could someone please explain to me in really simple terms what Static or Class Methods are?
Also, could I get a simple explanation on what Instance methods are?
This is what they give me in the textbook:
There are important practical implications of the presence or absence of the static modifier. A public class method may be invoked and executed as soon as Java processes the definition of the class to which it belongs. That is not the case for an instance method. Before a public instance method may be invoked and executed, an instance must be created of the class to which it belongs. To use a public class method, you just need the class. On the other hand, before you can use a public instance method you must have an instance of the class.
The manner in which a static method is invoked within the definition of another method varies according to whether or not the two methods belong to the same class. In the example above, factorial and main are both methods of the MainClass class. As a result, the invocation of factorial in the definition of main simply references the method name, "factorial".
java static static-methods non-static
add a comment |
up vote
62
down vote
favorite
up vote
62
down vote
favorite
I was just reading over the text given to me in my textbook and I'm not really sure I understand what it is saying. It's basically telling me that static methods or class methods include the "modifier" keyword static. But I don't really know what that means?
Could someone please explain to me in really simple terms what Static or Class Methods are?
Also, could I get a simple explanation on what Instance methods are?
This is what they give me in the textbook:
There are important practical implications of the presence or absence of the static modifier. A public class method may be invoked and executed as soon as Java processes the definition of the class to which it belongs. That is not the case for an instance method. Before a public instance method may be invoked and executed, an instance must be created of the class to which it belongs. To use a public class method, you just need the class. On the other hand, before you can use a public instance method you must have an instance of the class.
The manner in which a static method is invoked within the definition of another method varies according to whether or not the two methods belong to the same class. In the example above, factorial and main are both methods of the MainClass class. As a result, the invocation of factorial in the definition of main simply references the method name, "factorial".
java static static-methods non-static
I was just reading over the text given to me in my textbook and I'm not really sure I understand what it is saying. It's basically telling me that static methods or class methods include the "modifier" keyword static. But I don't really know what that means?
Could someone please explain to me in really simple terms what Static or Class Methods are?
Also, could I get a simple explanation on what Instance methods are?
This is what they give me in the textbook:
There are important practical implications of the presence or absence of the static modifier. A public class method may be invoked and executed as soon as Java processes the definition of the class to which it belongs. That is not the case for an instance method. Before a public instance method may be invoked and executed, an instance must be created of the class to which it belongs. To use a public class method, you just need the class. On the other hand, before you can use a public instance method you must have an instance of the class.
The manner in which a static method is invoked within the definition of another method varies according to whether or not the two methods belong to the same class. In the example above, factorial and main are both methods of the MainClass class. As a result, the invocation of factorial in the definition of main simply references the method name, "factorial".
java static static-methods non-static
java static static-methods non-static
edited Nov 24 '14 at 23:10
Platinum Azure
34.4k490122
34.4k490122
asked Aug 16 '12 at 18:15
Panthy
63021226
63021226
add a comment |
add a comment |
10 Answers
10
active
oldest
votes
up vote
114
down vote
accepted
The basic paradigm in Java is that you write classes, and that those classes are instantiated. Instantiated objects (an instance of a class) have attributes associated with them (member variables) that affect their behavior; when the instance has its method executed it will refer to these variables.
However, all objects of a particular type might have behavior that is not dependent at all on member variables; these methods are best made static. By being static, no instance of the class is required to run the method.
You can do this to execute a static method:
MyObject.staticMethod();//Simply refers to the class's static code
But to execute a non-static method, you must do this:
MyObject obj = new MyObject();//Create an instance
obj.nonstaticMethod();//Refer to the instance's class's code
On a deeper level, when the compiler puts a class together, it contains several pointers to methods. When those methods are executed it follows the pointers and executes the code at the far end. If a class is instantiated, the created object contains a pointer to the "virtual method table", which points to the methods to be called for that particular class in the inheritance hierarchy. However, if the method is static, no "virtual method table" is needed: all calls to that method go to the exact same place in memory to execute the exact same code. For that reason, in high-performance systems it's better to use a static method if you are not reliant on instance variables.
2
thank you very much for the code examples they look familiar so i understand now
– Panthy
Aug 16 '12 at 18:36
3
@Rohan Another good way of looking at it is that anythingstatic
(a class, method, or field) doesn't really belong to anything, it just hangs out in the class for organizational purposes.
– gobernador
Aug 16 '12 at 18:41
1
in addition static fields are used as data objects(one which holds the data and therefore cant change but you can manipulate data inside it.).Static methods can only be called upon static fields and henceforth carries the same notion of staying constant or not changeing or not instantiating !!
– user2416728
Jul 1 '13 at 6:17
1
@user2416728 Your comment is very confused. Static fields can be changed, but their scope is global to their execution environment. Therefore, any code that has access to said field has access to the same data. This is not equivalent to that data staying constant (unless a 'final' modifier is used).
– Nathaniel Ford
Jul 1 '13 at 16:24
2
aye,"not change" >> i meant cannot be instantiated !!
– user2416728
Jul 2 '13 at 8:18
add a comment |
up vote
13
down vote
Methods and variables that are not declared as static are known as instance methods and instance variables. To refer to instance methods and variables, you must instantiate the class first means you should create an object of that class first.For static you don't need to instantiate the class u can access the methods and variables with the class name using period sign which is in (.)
for example:
Person.staticMethod(); //accessing static method.
for non-static method you must instantiate the class.
Person person1 = new Person(); //instantiating
person1.nonStaticMethod(); //accessing non-static method.
add a comment |
up vote
7
down vote
Static methods, variables belongs to the whole class, not just an object instance. A static method, variable is associated with the class as a whole rather than with specific instances of a class. Each object will share a common copy of the static methods, variables. There is only one copy per class, no matter how many objects are created from it.
add a comment |
up vote
4
down vote
Instance methods => invoked on specific instance of a specific class. Method wants to know upon which class it was invoked. The way it happens there is a invisible parameter called 'this'. Inside of 'this' we have members of instance class already set with values. 'This' is not a variable. It's a value, you cannot change it and the value is reference to the receiver of the call.
Ex: You call repairmen(instance method) to fix your TV(actual program). He comes with tools('this' parameter). He comes with specific tools needed for fixing TV and he can fix other things also.
In static methods => there is no such thing as 'this'.
Ex: The same repairman (static method). When you call him you have to specify which repairman to call(like electrician). And he will come and fix your TV only. But, he doesn't have tools to fix other things (there is no 'this' parameter).
Static methods are usually useful for operations that don't require any data from an instance of the class (from 'this') and can perform their intended purpose solely using their arguments.
add a comment |
up vote
4
down vote
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.
Reference : geeksforgeeks
add a comment |
up vote
1
down vote
If state of a method is not supposed to be changed or its not going to use any instance variables.
You want to call method without instance.
If it only works on arguments provided to it.
Utility functions are good instance of static methods. i.e math.pow(), this function is not going to change the state for different values. So it is static.
add a comment |
up vote
1
down vote
The behavior of an object depends on the variables and the methods of that class. When we create a class we create an object for it. For static methods, we don't require them as static methods means all the objects will have the same copy so there is no need of an object.
e.g:
Myclass.get();
In instance method each object will have different behaviour so they have to call the method using the object instance.
e.g:
Myclass x = new Myclass();
x.get();
add a comment |
up vote
1
down vote
Instance method vs Static method
Instance method can access the instance methods and instance
variables directly.Instance method can access static variables and static methods
directly.Static methods can access the static variables and static methods
directly.Static methods can’t access instance methods and instance variables
directly. They must use reference to object. And static method can’t
use this keyword as there is no instance for ‘this’ to refer to.
add a comment |
up vote
0
down vote
In short, static methods and static variables are class level where as instance methods and instance variables are instance or object level.
This means whenever a instance or object (using new ClassName()) is created, this object will retain its own copy of instace variables. If you have five different objects of same class, you will have five different copies of the instance variables. But the static variables and methods will be the same for all those five objects. If you need something common to be used by each object created make it static. If you need a method which won't need object specific data to work, make it static. The static method will only work with static variable or will return data on the basis of passed arguments.
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() as:
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 first class, add() will return the sum of data passed by a specific object. But the static method can be used to get the sum from any class not independent if any specific instance or object. Hence, for generic methods which only need arguments to work can be made static to keep it all DRY.
add a comment |
up vote
-1
down vote
The static modifier when placed in front of a function implies that only one copy of that function exists. If the static modifier is not placed in front of the function then with every object or instance of that class a new copy of that function is made. :)
Same is the case with variables.
1
No, this is incorrect. Only the variables are copied.
– Robin Green
Jan 11 '14 at 11:46
add a comment |
protected by Josh Crozier Aug 10 '17 at 21:53
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
10 Answers
10
active
oldest
votes
10 Answers
10
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
114
down vote
accepted
The basic paradigm in Java is that you write classes, and that those classes are instantiated. Instantiated objects (an instance of a class) have attributes associated with them (member variables) that affect their behavior; when the instance has its method executed it will refer to these variables.
However, all objects of a particular type might have behavior that is not dependent at all on member variables; these methods are best made static. By being static, no instance of the class is required to run the method.
You can do this to execute a static method:
MyObject.staticMethod();//Simply refers to the class's static code
But to execute a non-static method, you must do this:
MyObject obj = new MyObject();//Create an instance
obj.nonstaticMethod();//Refer to the instance's class's code
On a deeper level, when the compiler puts a class together, it contains several pointers to methods. When those methods are executed it follows the pointers and executes the code at the far end. If a class is instantiated, the created object contains a pointer to the "virtual method table", which points to the methods to be called for that particular class in the inheritance hierarchy. However, if the method is static, no "virtual method table" is needed: all calls to that method go to the exact same place in memory to execute the exact same code. For that reason, in high-performance systems it's better to use a static method if you are not reliant on instance variables.
2
thank you very much for the code examples they look familiar so i understand now
– Panthy
Aug 16 '12 at 18:36
3
@Rohan Another good way of looking at it is that anythingstatic
(a class, method, or field) doesn't really belong to anything, it just hangs out in the class for organizational purposes.
– gobernador
Aug 16 '12 at 18:41
1
in addition static fields are used as data objects(one which holds the data and therefore cant change but you can manipulate data inside it.).Static methods can only be called upon static fields and henceforth carries the same notion of staying constant or not changeing or not instantiating !!
– user2416728
Jul 1 '13 at 6:17
1
@user2416728 Your comment is very confused. Static fields can be changed, but their scope is global to their execution environment. Therefore, any code that has access to said field has access to the same data. This is not equivalent to that data staying constant (unless a 'final' modifier is used).
– Nathaniel Ford
Jul 1 '13 at 16:24
2
aye,"not change" >> i meant cannot be instantiated !!
– user2416728
Jul 2 '13 at 8:18
add a comment |
up vote
114
down vote
accepted
The basic paradigm in Java is that you write classes, and that those classes are instantiated. Instantiated objects (an instance of a class) have attributes associated with them (member variables) that affect their behavior; when the instance has its method executed it will refer to these variables.
However, all objects of a particular type might have behavior that is not dependent at all on member variables; these methods are best made static. By being static, no instance of the class is required to run the method.
You can do this to execute a static method:
MyObject.staticMethod();//Simply refers to the class's static code
But to execute a non-static method, you must do this:
MyObject obj = new MyObject();//Create an instance
obj.nonstaticMethod();//Refer to the instance's class's code
On a deeper level, when the compiler puts a class together, it contains several pointers to methods. When those methods are executed it follows the pointers and executes the code at the far end. If a class is instantiated, the created object contains a pointer to the "virtual method table", which points to the methods to be called for that particular class in the inheritance hierarchy. However, if the method is static, no "virtual method table" is needed: all calls to that method go to the exact same place in memory to execute the exact same code. For that reason, in high-performance systems it's better to use a static method if you are not reliant on instance variables.
2
thank you very much for the code examples they look familiar so i understand now
– Panthy
Aug 16 '12 at 18:36
3
@Rohan Another good way of looking at it is that anythingstatic
(a class, method, or field) doesn't really belong to anything, it just hangs out in the class for organizational purposes.
– gobernador
Aug 16 '12 at 18:41
1
in addition static fields are used as data objects(one which holds the data and therefore cant change but you can manipulate data inside it.).Static methods can only be called upon static fields and henceforth carries the same notion of staying constant or not changeing or not instantiating !!
– user2416728
Jul 1 '13 at 6:17
1
@user2416728 Your comment is very confused. Static fields can be changed, but their scope is global to their execution environment. Therefore, any code that has access to said field has access to the same data. This is not equivalent to that data staying constant (unless a 'final' modifier is used).
– Nathaniel Ford
Jul 1 '13 at 16:24
2
aye,"not change" >> i meant cannot be instantiated !!
– user2416728
Jul 2 '13 at 8:18
add a comment |
up vote
114
down vote
accepted
up vote
114
down vote
accepted
The basic paradigm in Java is that you write classes, and that those classes are instantiated. Instantiated objects (an instance of a class) have attributes associated with them (member variables) that affect their behavior; when the instance has its method executed it will refer to these variables.
However, all objects of a particular type might have behavior that is not dependent at all on member variables; these methods are best made static. By being static, no instance of the class is required to run the method.
You can do this to execute a static method:
MyObject.staticMethod();//Simply refers to the class's static code
But to execute a non-static method, you must do this:
MyObject obj = new MyObject();//Create an instance
obj.nonstaticMethod();//Refer to the instance's class's code
On a deeper level, when the compiler puts a class together, it contains several pointers to methods. When those methods are executed it follows the pointers and executes the code at the far end. If a class is instantiated, the created object contains a pointer to the "virtual method table", which points to the methods to be called for that particular class in the inheritance hierarchy. However, if the method is static, no "virtual method table" is needed: all calls to that method go to the exact same place in memory to execute the exact same code. For that reason, in high-performance systems it's better to use a static method if you are not reliant on instance variables.
The basic paradigm in Java is that you write classes, and that those classes are instantiated. Instantiated objects (an instance of a class) have attributes associated with them (member variables) that affect their behavior; when the instance has its method executed it will refer to these variables.
However, all objects of a particular type might have behavior that is not dependent at all on member variables; these methods are best made static. By being static, no instance of the class is required to run the method.
You can do this to execute a static method:
MyObject.staticMethod();//Simply refers to the class's static code
But to execute a non-static method, you must do this:
MyObject obj = new MyObject();//Create an instance
obj.nonstaticMethod();//Refer to the instance's class's code
On a deeper level, when the compiler puts a class together, it contains several pointers to methods. When those methods are executed it follows the pointers and executes the code at the far end. If a class is instantiated, the created object contains a pointer to the "virtual method table", which points to the methods to be called for that particular class in the inheritance hierarchy. However, if the method is static, no "virtual method table" is needed: all calls to that method go to the exact same place in memory to execute the exact same code. For that reason, in high-performance systems it's better to use a static method if you are not reliant on instance variables.
edited Jan 11 '14 at 11:44
Robin Green
22.1k875152
22.1k875152
answered Aug 16 '12 at 18:18
Nathaniel Ford
13.3k145272
13.3k145272
2
thank you very much for the code examples they look familiar so i understand now
– Panthy
Aug 16 '12 at 18:36
3
@Rohan Another good way of looking at it is that anythingstatic
(a class, method, or field) doesn't really belong to anything, it just hangs out in the class for organizational purposes.
– gobernador
Aug 16 '12 at 18:41
1
in addition static fields are used as data objects(one which holds the data and therefore cant change but you can manipulate data inside it.).Static methods can only be called upon static fields and henceforth carries the same notion of staying constant or not changeing or not instantiating !!
– user2416728
Jul 1 '13 at 6:17
1
@user2416728 Your comment is very confused. Static fields can be changed, but their scope is global to their execution environment. Therefore, any code that has access to said field has access to the same data. This is not equivalent to that data staying constant (unless a 'final' modifier is used).
– Nathaniel Ford
Jul 1 '13 at 16:24
2
aye,"not change" >> i meant cannot be instantiated !!
– user2416728
Jul 2 '13 at 8:18
add a comment |
2
thank you very much for the code examples they look familiar so i understand now
– Panthy
Aug 16 '12 at 18:36
3
@Rohan Another good way of looking at it is that anythingstatic
(a class, method, or field) doesn't really belong to anything, it just hangs out in the class for organizational purposes.
– gobernador
Aug 16 '12 at 18:41
1
in addition static fields are used as data objects(one which holds the data and therefore cant change but you can manipulate data inside it.).Static methods can only be called upon static fields and henceforth carries the same notion of staying constant or not changeing or not instantiating !!
– user2416728
Jul 1 '13 at 6:17
1
@user2416728 Your comment is very confused. Static fields can be changed, but their scope is global to their execution environment. Therefore, any code that has access to said field has access to the same data. This is not equivalent to that data staying constant (unless a 'final' modifier is used).
– Nathaniel Ford
Jul 1 '13 at 16:24
2
aye,"not change" >> i meant cannot be instantiated !!
– user2416728
Jul 2 '13 at 8:18
2
2
thank you very much for the code examples they look familiar so i understand now
– Panthy
Aug 16 '12 at 18:36
thank you very much for the code examples they look familiar so i understand now
– Panthy
Aug 16 '12 at 18:36
3
3
@Rohan Another good way of looking at it is that anything
static
(a class, method, or field) doesn't really belong to anything, it just hangs out in the class for organizational purposes.– gobernador
Aug 16 '12 at 18:41
@Rohan Another good way of looking at it is that anything
static
(a class, method, or field) doesn't really belong to anything, it just hangs out in the class for organizational purposes.– gobernador
Aug 16 '12 at 18:41
1
1
in addition static fields are used as data objects(one which holds the data and therefore cant change but you can manipulate data inside it.).Static methods can only be called upon static fields and henceforth carries the same notion of staying constant or not changeing or not instantiating !!
– user2416728
Jul 1 '13 at 6:17
in addition static fields are used as data objects(one which holds the data and therefore cant change but you can manipulate data inside it.).Static methods can only be called upon static fields and henceforth carries the same notion of staying constant or not changeing or not instantiating !!
– user2416728
Jul 1 '13 at 6:17
1
1
@user2416728 Your comment is very confused. Static fields can be changed, but their scope is global to their execution environment. Therefore, any code that has access to said field has access to the same data. This is not equivalent to that data staying constant (unless a 'final' modifier is used).
– Nathaniel Ford
Jul 1 '13 at 16:24
@user2416728 Your comment is very confused. Static fields can be changed, but their scope is global to their execution environment. Therefore, any code that has access to said field has access to the same data. This is not equivalent to that data staying constant (unless a 'final' modifier is used).
– Nathaniel Ford
Jul 1 '13 at 16:24
2
2
aye,"not change" >> i meant cannot be instantiated !!
– user2416728
Jul 2 '13 at 8:18
aye,"not change" >> i meant cannot be instantiated !!
– user2416728
Jul 2 '13 at 8:18
add a comment |
up vote
13
down vote
Methods and variables that are not declared as static are known as instance methods and instance variables. To refer to instance methods and variables, you must instantiate the class first means you should create an object of that class first.For static you don't need to instantiate the class u can access the methods and variables with the class name using period sign which is in (.)
for example:
Person.staticMethod(); //accessing static method.
for non-static method you must instantiate the class.
Person person1 = new Person(); //instantiating
person1.nonStaticMethod(); //accessing non-static method.
add a comment |
up vote
13
down vote
Methods and variables that are not declared as static are known as instance methods and instance variables. To refer to instance methods and variables, you must instantiate the class first means you should create an object of that class first.For static you don't need to instantiate the class u can access the methods and variables with the class name using period sign which is in (.)
for example:
Person.staticMethod(); //accessing static method.
for non-static method you must instantiate the class.
Person person1 = new Person(); //instantiating
person1.nonStaticMethod(); //accessing non-static method.
add a comment |
up vote
13
down vote
up vote
13
down vote
Methods and variables that are not declared as static are known as instance methods and instance variables. To refer to instance methods and variables, you must instantiate the class first means you should create an object of that class first.For static you don't need to instantiate the class u can access the methods and variables with the class name using period sign which is in (.)
for example:
Person.staticMethod(); //accessing static method.
for non-static method you must instantiate the class.
Person person1 = new Person(); //instantiating
person1.nonStaticMethod(); //accessing non-static method.
Methods and variables that are not declared as static are known as instance methods and instance variables. To refer to instance methods and variables, you must instantiate the class first means you should create an object of that class first.For static you don't need to instantiate the class u can access the methods and variables with the class name using period sign which is in (.)
for example:
Person.staticMethod(); //accessing static method.
for non-static method you must instantiate the class.
Person person1 = new Person(); //instantiating
person1.nonStaticMethod(); //accessing non-static method.
edited Mar 7 '16 at 12:06
Sangram S.
2,50321632
2,50321632
answered Jul 3 '15 at 11:24
Khalid Ahmed Khichi
13113
13113
add a comment |
add a comment |
up vote
7
down vote
Static methods, variables belongs to the whole class, not just an object instance. A static method, variable is associated with the class as a whole rather than with specific instances of a class. Each object will share a common copy of the static methods, variables. There is only one copy per class, no matter how many objects are created from it.
add a comment |
up vote
7
down vote
Static methods, variables belongs to the whole class, not just an object instance. A static method, variable is associated with the class as a whole rather than with specific instances of a class. Each object will share a common copy of the static methods, variables. There is only one copy per class, no matter how many objects are created from it.
add a comment |
up vote
7
down vote
up vote
7
down vote
Static methods, variables belongs to the whole class, not just an object instance. A static method, variable is associated with the class as a whole rather than with specific instances of a class. Each object will share a common copy of the static methods, variables. There is only one copy per class, no matter how many objects are created from it.
Static methods, variables belongs to the whole class, not just an object instance. A static method, variable is associated with the class as a whole rather than with specific instances of a class. Each object will share a common copy of the static methods, variables. There is only one copy per class, no matter how many objects are created from it.
answered Aug 16 '12 at 18:24
Paulius Matulionis
15.2k1888130
15.2k1888130
add a comment |
add a comment |
up vote
4
down vote
Instance methods => invoked on specific instance of a specific class. Method wants to know upon which class it was invoked. The way it happens there is a invisible parameter called 'this'. Inside of 'this' we have members of instance class already set with values. 'This' is not a variable. It's a value, you cannot change it and the value is reference to the receiver of the call.
Ex: You call repairmen(instance method) to fix your TV(actual program). He comes with tools('this' parameter). He comes with specific tools needed for fixing TV and he can fix other things also.
In static methods => there is no such thing as 'this'.
Ex: The same repairman (static method). When you call him you have to specify which repairman to call(like electrician). And he will come and fix your TV only. But, he doesn't have tools to fix other things (there is no 'this' parameter).
Static methods are usually useful for operations that don't require any data from an instance of the class (from 'this') and can perform their intended purpose solely using their arguments.
add a comment |
up vote
4
down vote
Instance methods => invoked on specific instance of a specific class. Method wants to know upon which class it was invoked. The way it happens there is a invisible parameter called 'this'. Inside of 'this' we have members of instance class already set with values. 'This' is not a variable. It's a value, you cannot change it and the value is reference to the receiver of the call.
Ex: You call repairmen(instance method) to fix your TV(actual program). He comes with tools('this' parameter). He comes with specific tools needed for fixing TV and he can fix other things also.
In static methods => there is no such thing as 'this'.
Ex: The same repairman (static method). When you call him you have to specify which repairman to call(like electrician). And he will come and fix your TV only. But, he doesn't have tools to fix other things (there is no 'this' parameter).
Static methods are usually useful for operations that don't require any data from an instance of the class (from 'this') and can perform their intended purpose solely using their arguments.
add a comment |
up vote
4
down vote
up vote
4
down vote
Instance methods => invoked on specific instance of a specific class. Method wants to know upon which class it was invoked. The way it happens there is a invisible parameter called 'this'. Inside of 'this' we have members of instance class already set with values. 'This' is not a variable. It's a value, you cannot change it and the value is reference to the receiver of the call.
Ex: You call repairmen(instance method) to fix your TV(actual program). He comes with tools('this' parameter). He comes with specific tools needed for fixing TV and he can fix other things also.
In static methods => there is no such thing as 'this'.
Ex: The same repairman (static method). When you call him you have to specify which repairman to call(like electrician). And he will come and fix your TV only. But, he doesn't have tools to fix other things (there is no 'this' parameter).
Static methods are usually useful for operations that don't require any data from an instance of the class (from 'this') and can perform their intended purpose solely using their arguments.
Instance methods => invoked on specific instance of a specific class. Method wants to know upon which class it was invoked. The way it happens there is a invisible parameter called 'this'. Inside of 'this' we have members of instance class already set with values. 'This' is not a variable. It's a value, you cannot change it and the value is reference to the receiver of the call.
Ex: You call repairmen(instance method) to fix your TV(actual program). He comes with tools('this' parameter). He comes with specific tools needed for fixing TV and he can fix other things also.
In static methods => there is no such thing as 'this'.
Ex: The same repairman (static method). When you call him you have to specify which repairman to call(like electrician). And he will come and fix your TV only. But, he doesn't have tools to fix other things (there is no 'this' parameter).
Static methods are usually useful for operations that don't require any data from an instance of the class (from 'this') and can perform their intended purpose solely using their arguments.
edited Aug 22 '16 at 20:33
answered Aug 22 '16 at 15:35
Eldiyar Talantbek
50456
50456
add a comment |
add a comment |
up vote
4
down vote
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.
Reference : geeksforgeeks
add a comment |
up vote
4
down vote
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.
Reference : geeksforgeeks
add a comment |
up vote
4
down vote
up vote
4
down vote
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.
Reference : geeksforgeeks
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.
Reference : geeksforgeeks
edited Apr 2 '17 at 18:29
answered Apr 2 '17 at 18:17
rashedcs
1,0241224
1,0241224
add a comment |
add a comment |
up vote
1
down vote
If state of a method is not supposed to be changed or its not going to use any instance variables.
You want to call method without instance.
If it only works on arguments provided to it.
Utility functions are good instance of static methods. i.e math.pow(), this function is not going to change the state for different values. So it is static.
add a comment |
up vote
1
down vote
If state of a method is not supposed to be changed or its not going to use any instance variables.
You want to call method without instance.
If it only works on arguments provided to it.
Utility functions are good instance of static methods. i.e math.pow(), this function is not going to change the state for different values. So it is static.
add a comment |
up vote
1
down vote
up vote
1
down vote
If state of a method is not supposed to be changed or its not going to use any instance variables.
You want to call method without instance.
If it only works on arguments provided to it.
Utility functions are good instance of static methods. i.e math.pow(), this function is not going to change the state for different values. So it is static.
If state of a method is not supposed to be changed or its not going to use any instance variables.
You want to call method without instance.
If it only works on arguments provided to it.
Utility functions are good instance of static methods. i.e math.pow(), this function is not going to change the state for different values. So it is static.
answered Aug 16 '16 at 0:20
Suketu Patel
323610
323610
add a comment |
add a comment |
up vote
1
down vote
The behavior of an object depends on the variables and the methods of that class. When we create a class we create an object for it. For static methods, we don't require them as static methods means all the objects will have the same copy so there is no need of an object.
e.g:
Myclass.get();
In instance method each object will have different behaviour so they have to call the method using the object instance.
e.g:
Myclass x = new Myclass();
x.get();
add a comment |
up vote
1
down vote
The behavior of an object depends on the variables and the methods of that class. When we create a class we create an object for it. For static methods, we don't require them as static methods means all the objects will have the same copy so there is no need of an object.
e.g:
Myclass.get();
In instance method each object will have different behaviour so they have to call the method using the object instance.
e.g:
Myclass x = new Myclass();
x.get();
add a comment |
up vote
1
down vote
up vote
1
down vote
The behavior of an object depends on the variables and the methods of that class. When we create a class we create an object for it. For static methods, we don't require them as static methods means all the objects will have the same copy so there is no need of an object.
e.g:
Myclass.get();
In instance method each object will have different behaviour so they have to call the method using the object instance.
e.g:
Myclass x = new Myclass();
x.get();
The behavior of an object depends on the variables and the methods of that class. When we create a class we create an object for it. For static methods, we don't require them as static methods means all the objects will have the same copy so there is no need of an object.
e.g:
Myclass.get();
In instance method each object will have different behaviour so they have to call the method using the object instance.
e.g:
Myclass x = new Myclass();
x.get();
edited Feb 1 '17 at 0:20
xlm
2,92992837
2,92992837
answered Apr 19 '16 at 5:35
Vishal Patil
547
547
add a comment |
add a comment |
up vote
1
down vote
Instance method vs Static method
Instance method can access the instance methods and instance
variables directly.Instance method can access static variables and static methods
directly.Static methods can access the static variables and static methods
directly.Static methods can’t access instance methods and instance variables
directly. They must use reference to object. And static method can’t
use this keyword as there is no instance for ‘this’ to refer to.
add a comment |
up vote
1
down vote
Instance method vs Static method
Instance method can access the instance methods and instance
variables directly.Instance method can access static variables and static methods
directly.Static methods can access the static variables and static methods
directly.Static methods can’t access instance methods and instance variables
directly. They must use reference to object. And static method can’t
use this keyword as there is no instance for ‘this’ to refer to.
add a comment |
up vote
1
down vote
up vote
1
down vote
Instance method vs Static method
Instance method can access the instance methods and instance
variables directly.Instance method can access static variables and static methods
directly.Static methods can access the static variables and static methods
directly.Static methods can’t access instance methods and instance variables
directly. They must use reference to object. And static method can’t
use this keyword as there is no instance for ‘this’ to refer to.
Instance method vs Static method
Instance method can access the instance methods and instance
variables directly.Instance method can access static variables and static methods
directly.Static methods can access the static variables and static methods
directly.Static methods can’t access instance methods and instance variables
directly. They must use reference to object. And static method can’t
use this keyword as there is no instance for ‘this’ to refer to.
answered Aug 6 at 8:28
param
293617
293617
add a comment |
add a comment |
up vote
0
down vote
In short, static methods and static variables are class level where as instance methods and instance variables are instance or object level.
This means whenever a instance or object (using new ClassName()) is created, this object will retain its own copy of instace variables. If you have five different objects of same class, you will have five different copies of the instance variables. But the static variables and methods will be the same for all those five objects. If you need something common to be used by each object created make it static. If you need a method which won't need object specific data to work, make it static. The static method will only work with static variable or will return data on the basis of passed arguments.
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() as:
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 first class, add() will return the sum of data passed by a specific object. But the static method can be used to get the sum from any class not independent if any specific instance or object. Hence, for generic methods which only need arguments to work can be made static to keep it all DRY.
add a comment |
up vote
0
down vote
In short, static methods and static variables are class level where as instance methods and instance variables are instance or object level.
This means whenever a instance or object (using new ClassName()) is created, this object will retain its own copy of instace variables. If you have five different objects of same class, you will have five different copies of the instance variables. But the static variables and methods will be the same for all those five objects. If you need something common to be used by each object created make it static. If you need a method which won't need object specific data to work, make it static. The static method will only work with static variable or will return data on the basis of passed arguments.
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() as:
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 first class, add() will return the sum of data passed by a specific object. But the static method can be used to get the sum from any class not independent if any specific instance or object. Hence, for generic methods which only need arguments to work can be made static to keep it all DRY.
add a comment |
up vote
0
down vote
up vote
0
down vote
In short, static methods and static variables are class level where as instance methods and instance variables are instance or object level.
This means whenever a instance or object (using new ClassName()) is created, this object will retain its own copy of instace variables. If you have five different objects of same class, you will have five different copies of the instance variables. But the static variables and methods will be the same for all those five objects. If you need something common to be used by each object created make it static. If you need a method which won't need object specific data to work, make it static. The static method will only work with static variable or will return data on the basis of passed arguments.
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() as:
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 first class, add() will return the sum of data passed by a specific object. But the static method can be used to get the sum from any class not independent if any specific instance or object. Hence, for generic methods which only need arguments to work can be made static to keep it all DRY.
In short, static methods and static variables are class level where as instance methods and instance variables are instance or object level.
This means whenever a instance or object (using new ClassName()) is created, this object will retain its own copy of instace variables. If you have five different objects of same class, you will have five different copies of the instance variables. But the static variables and methods will be the same for all those five objects. If you need something common to be used by each object created make it static. If you need a method which won't need object specific data to work, make it static. The static method will only work with static variable or will return data on the basis of passed arguments.
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() as:
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 first class, add() will return the sum of data passed by a specific object. But the static method can be used to get the sum from any class not independent if any specific instance or object. Hence, for generic methods which only need arguments to work can be made static to keep it all DRY.
answered Jun 22 '17 at 4:27
Prakash
121110
121110
add a comment |
add a comment |
up vote
-1
down vote
The static modifier when placed in front of a function implies that only one copy of that function exists. If the static modifier is not placed in front of the function then with every object or instance of that class a new copy of that function is made. :)
Same is the case with variables.
1
No, this is incorrect. Only the variables are copied.
– Robin Green
Jan 11 '14 at 11:46
add a comment |
up vote
-1
down vote
The static modifier when placed in front of a function implies that only one copy of that function exists. If the static modifier is not placed in front of the function then with every object or instance of that class a new copy of that function is made. :)
Same is the case with variables.
1
No, this is incorrect. Only the variables are copied.
– Robin Green
Jan 11 '14 at 11:46
add a comment |
up vote
-1
down vote
up vote
-1
down vote
The static modifier when placed in front of a function implies that only one copy of that function exists. If the static modifier is not placed in front of the function then with every object or instance of that class a new copy of that function is made. :)
Same is the case with variables.
The static modifier when placed in front of a function implies that only one copy of that function exists. If the static modifier is not placed in front of the function then with every object or instance of that class a new copy of that function is made. :)
Same is the case with variables.
answered Aug 16 '12 at 18:20
Prakhar Mohan Srivastava
1,9021650107
1,9021650107
1
No, this is incorrect. Only the variables are copied.
– Robin Green
Jan 11 '14 at 11:46
add a comment |
1
No, this is incorrect. Only the variables are copied.
– Robin Green
Jan 11 '14 at 11:46
1
1
No, this is incorrect. Only the variables are copied.
– Robin Green
Jan 11 '14 at 11:46
No, this is incorrect. Only the variables are copied.
– Robin Green
Jan 11 '14 at 11:46
add a comment |
protected by Josh Crozier Aug 10 '17 at 21:53
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?