I was in interview, the interviewer asked me this question, I couldn't satisfy him with proper answers he was looking for, Need help, Thanks
-
A static variable is instantiated once, a static method can be called from a class without instantiating an instance of the class, a static class cannot be instantiated. – Ryan Wilson Oct 22 '18 at 18:23
-
1@RyanWilson static classes can be instantiated. You can't make a top-level class static, but when you make a nested class static, it can be instantiated independent of its parent. – TheWanderer Oct 22 '18 at 18:25
-
@RyanWilson https://pastebin.com/vsmG1vb4 – TheWanderer Oct 22 '18 at 18:31
-
Not really a good duplicate. `static` means that the field, method or class is not bound to an instance of the enclosing class which would otherwise be the case. – zapl Oct 22 '18 at 18:45
2 Answers
A static variable is a variable:
public static int whatever = 0;
A static method is a method:
public static void whatever() {}
A static class is a class:
public static class SomeInnerClass {}
(A class can only have the static
modifier when it is nested inside another class)
Static variables and methods can be accessed from any other class, and aren't tied to the instance of that class. For instance, say you have the following class:
public class SomeClass {
public static int myInt = 0;
public static int add(int one, int two) {
return one + two;
}
}
From any other class, you can access the variable and method directly, without creating an instance of SomeClass
:
SomeClass.myInt = 23;
int sum = SomeClass.add(SomeClass.myInt, 2); //this will equal 25
If the variable and method weren't static, you'd have to first instantiate SomeClass and then reference that instance:
SomeClass someClass = new SomeClass();
someClass.myInt = 23;
int sum = someClass.add(someClass.myInt, 2); //this will equal 25
Static classes are used to separate a nested class from its parent and remove the dependency on that parent's instance. Take the following code:
public class ParentClass {
public class ChildClass {}
}
From another (non-child of Parent) class, you would be able to use:
ParentCLass parent = new ParentClass();
But you wouldn't be able to do:
ChildClass child = new ChildClass(); //this won't compile if it's not in ParentClass
However, if ChildClass becomes static:
public class ParentClass {
public static class ChildClass {}
}
You will be able to instantiate it from another (non-child of Parent) class:
ChildClass child = new ChildClass(); //this will compile when put in any class
I recommend reading some Java basics on how classes work: https://www.geeksforgeeks.org/classes-objects-java/

- 16,775
- 6
- 49
- 63
In Java, static is a keyword used to describe how objects are managed in memory. It means that the static object belongs specifically to the class, instead of instances of that class. Variables, methods, classes can be static. The difference between them is simply the same as usual(the difference between variables, methods, and classes). A static variable(container such as int or String), static method(a method that lives in memory and is part of the class(can be called without an instance of the class Hello.staticMethod()), a static class is a bit unique in Java look at this Static Classes In Java

- 17
- 1
- 6