Static Keyword
Static Keyword
nested/inner
classes.
Class Abc
{
static int a;
void m1()
{
Static int a; //wrong, local variable cannot be static.
}
}
System.out.println(e1.eid+" "+e1.name);
}
}
Static variable is used as counter for counting number of objects created during program execution.
class CounterDemo
{
static int count;
{
count++;
}
public static void main(String args[])
{
CounterDemo e1 = new CounterDemo();
CounterDemo e2 = new CounterDemo();
CounterDemo e3 = new CounterDemo();
System.out.println("total created objects: "+count);
}
}
//usage of this
class Abc{
int a=20;
Abc()
{ this("pankaj");
System.out.print(" pateriya");
}
Abc(String name)
{
System.out.print(name);
}
Abc(Abc obj3)
{
System.out.println(++obj3.a);
}
OutPut:
pankaj pateriya
21
22
23