code 1)
public class Cat {
private static int catCount = 0;
public static void setCatCount(int catCount) {
//write your code here
}
public static void main(String[] args) {
}
}
code 2)
public class Cat {
private static int catCount = 0;
public void setCatCount(int catCount) {
//write your code here
}
public static void main(String[] args) {
}
}
Kindly let me know how code 1 is diferent from code 2
Under discussion
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
Attila
19 March 2020, 21:11
A static method belongs to the class itself while a non-static method belongs to each instance of a class. (When you Crate a new cat: " Cat cat = new Cat(); "Therefore, a static method can be called directly without creating any instance of the class and an object is needed to call a non-static method.
Let me show you. With Static method, to use the method u can just call the method:
With non static method, u need to create an Object, and call the method through the object:
The cat.setCatCuont(); method belongs to the cat object we just created.
the static setCatCount(); belongs to the whole class. +3
andy 6473
6 April 2020, 15:32
Thank you for providing me with the solution
+1