+ 26
[DUPLICATE] what is static and local in java ?
explanation with examples in detail.
6 Answers
+ 13
Static means that are part of the class . Every instance of an object share the same variable .
Local variables are usually used in method declarations and after the method call they get out of scope
+ 7
In Java, a static member is a member of a class that isnât associated with an instance of a class. Instead, the member belongs to the class itself. As a result, you can access the static member without first creating a class instance.
Any variable declared within a method is supposed to be local to the method.
+ 6
Like C/C++, static local variables are not allowed in Java. For example, following Java program fails in compilation with error âStatic local variables are not allowedâ
class Test {
   public static void main(String args[]) {
     System.out.println(fun());
   }
   static int fun()
   {
     static int x= 10; //Error: Static local variables are not allowed
     return x--;
   }
}
+ 4
Example: hope you Understand!!
https://code.sololearn.com/clhxNlV8ASSq/?ref=app
+ 3
static means class level variable.
all objects of the class shares static variables.
by default it is initialized by defaults values.
it is stored in method area.
local variables are the temporary variables.
variables declared inside method,constructor,blocks are called local variables.
it is mandatory to initialize local variables in Java.
it is stored in stack area and that's why it is also known as stack variables.