As discussed in Creating Static Methods In Java Using Static Keyword, we can have static methods and variables in a class. We can also have non-static methods and variables. These are nothing but the normal member variables and methods. If a variable is not marked as static, then it is non-static variable or in other words it is a member variable. If a method is not marked as static, then it is a non-static method.
Please find few rules about the access rules between static and non-static methods and variables.
class A A and created a variable i_static and a method printStatic as static and variable j_non_static and printNonStatic as non-static. The access rules are :
![]() Here we have one class A and 3 objects of the class, namely a1 , a2 and a3 . Since there is a separate copy of non-static variable for every object, we do not know which object's copy to use. So, when we call A.j_non_static , it does not know whether to use a1.j_non_static or a2.j_non_static or a3.j_non_static . But since i_static has only one copy and it is stored at the class level, either we access through class like A.i_static or through object using a1.i_static or a2.i_static or a3.i_static , all of them point to the same variable.
|