+ 26

[DUPLICATE] what is static and local in java ?

explanation with examples in detail.

4th Feb 2018, 11:05 AM
pratap dsp
pratap dsp - avatar
6 Answers
4th Feb 2018, 11:21 AM
Vukan
Vukan - avatar
+ 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
5th Feb 2018, 11:17 AM
Daniel Săvescu
Daniel Săvescu - avatar
+ 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.
4th Feb 2018, 11:23 AM
Diwakar
Diwakar - avatar
+ 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--;    } }
4th Feb 2018, 11:30 AM
Diwakar
Diwakar - avatar
+ 4
5th Feb 2018, 6:51 PM
P∆WAN M∆URY∆
P∆WAN M∆URY∆ - avatar
+ 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.
4th Feb 2018, 7:00 PM
Shubham Agrawal
Shubham Agrawal - avatar