Static Modifier
static is a modifier applicable for methods and variables but not for classes. we can't declare top-level classes with static modifiers but we can declare inner classes as static (such type of inner classes are called static nested classes).
- in the case of an instance variable for every object separate copy will be created but in the case of a static variable, a single copy will be created at class level and shared by every object of that class.
eg-:
class Test{
static int x=10;
int y=20;
public static void main(String[] args){
Test t=new Test();
t.x=222;
t.y=999;
Test t1=new Test();
System.out.print(t1.x+" "+t1.y);
}
}
static int x=10;
int y=20;
public static void main(String[] args){
Test t=new Test();
t.x=222;
t.y=999;
Test t1=new Test();
System.out.print(t1.x+" "+t1.y);
}
}
//output is-: 222 20
- we can't access instance members directly from the static area but we can access from the instance area directly.
eg-:
class Test{
int x=10;
public void m(){
System.out.print(x);// we can access directly because this is instance area
}
public static void main(String[] args){
System.out.print(x); //we cannot access it directly because this is a static area.
}
}
- we can access static members from both static and instance are access directly.
eg-:
class Test{
static int x=10;
public void m(){
System.out.print(x);
}
public static void main(String[] args){
System.out.print(x);
}
}
consider the following declaration.
- int x=10;
- static int x=10;
- public void m(){
System.out.print(x);
} - public static void m(){
System.out.print(x);
}
options-:
(a) . 1 & 3 True
(b) . 1 & 4 compile time error-: non static variable x cannot be referenced static context
(c) . 2 & 3 True
(d) . 2 & 4 True
(e) . 1 & 2 compile time error-: variable x is already define in Test
(f) . 3 & 3 compile time error -: m() is already define in Test
within the same class which of the above declarations, we can take simultaneously.
case 1-: overloading concept applicable for static method including main() but JVM always called main(String[] agrs) only.
other overloaded methods we have to call just like a normal method class.
eg-:
class Test{
public static void main(int[] args){
System.out.print("int[]");
}
public static void main(String[] args){
System.out.print("String[]");
}
}
// output-: String[]
case 2-: inheritance concept applicable for static method including main() hence while executing child class if child doesn't contain main() then parent class main() will be executed.
eg-:
class Test{
public static void main(String[] args){
System.out.print("parent method");
}
}
class P extends Test{
}
java Test -> press enter
o/p-: parent method
java P
o/p -: parent method
case 3-:it seems overriding applicable for the static method but it is not overriding and it is method hiding.
Note-:for static methods overloading and inheritance concepts of applicable but the overriding concepts are not applicable .but instead of overriding method hiding concepts are applicable.
class Test{
public static void main(String[] args){
System.out.print("parent method");
}
}
class P extends Test{
public static void main(String[] args){
System.out.print("child method");
}
}
java Test -> press enter
o/p-: parent method
java P
o/p -: child method
case 4-:Inside method implementation, if we are using at least one instance variable then that method talks about a particular object hence we should declare a method as an instance method.
Inside method implementation, if we are not using any instance variable then this method no way related to a particular object hence we have to declare a method as a static method. irrespective of whether we are using static variables or not.
eg-:
class Test{
int rollNo;
int marks;
String name;
static String collageName;
getStrudentInfo(){
return name+"......."+marks; //Instance method
}
getStudentCollageName(){
return collageName; //Static method
}
getAverage(int x,int y){
return (x+y)/2; //utility method
}
getCompleteInfo(){
return rollNo+".........."+marks+".........."+name+"........"+collageName; //Instance Method
}
}
case 5-:for the static method, the implementation should be available whereas for the abstract method implementation is not available hence abstract static combination is illegal for the method.