Google+

85. 'static' instance variables







Normally, members of a class must be accessed only in conjunction with an object of its class as shown in the below examples:
  • object1.volume;   -> Accessing instance variable 'volume' using 'object1' object 
  • object1.volume( ); -> Accessing volume( ) method using 'object1' object 
But if we specify the instance variable or method as 'static', then we can access them without reference to any object. The most common example of a static member is main( ). main( ) is declared as static because it must be called before any object exists.

public static void main(String args[])
{

}

Instance variables declared as 'static' are essentially global variables. When objects of its class are declared, no copy of static variable is made. Instead all the objects of the class share the same instance variable.

In order to access the static instance variables outside its class, we have to access them as ClassName.InstanceVariableName

Example:-    Test.a;  (Here Test is a class and 'a' is an instance variable )

Lets implement this on Eclipse IDE (static instance variables) :

1. Create a class 'StaticVariablesDemo' under 'eleventh_package' package of 'Eleventh Project' project as shown below:



2. Declare an instance variable of int type and specify it  as 'static' as shown below:



3. Create printVariable( ) method to print the value of the 'static' instance variable 'a' as shown below:



4. Create a class named 'AccessStaticVariables' under the same package 'eleventh_package' as shown below:



5. Access and print the value of 'static' instance variable 'a' of the 'StaticVariableDemo' class from  the 'AccessStaticVariables' class under the same package using ClassName.InstanceVariable as shown below:



6. Create an object 'object1' and Call the printVariable( ) method in 'StaticVariableDemo' class as shown below:



7. Save and Run the 'AccessStaticVaraibles' class as shown below:


8. Observe that the output is displayed in console as shown below:



At this point we've to understand that, we can access 'static' instance variables from the other class under the same package without actually creating an object. Lets try to find out whether can we access 'static' instance variables from a class under a different package of the same project by following the next steps.

9. Create a  class named  'DifferentPackageAccess' class under the 'different_package' package as shown below:



10. Access and print the value of 'static' instance variable 'a' of the 'StaticVariableDemo' class from  the 'DifferentPackageAccess' class under a different package of the same project  using ClassName.InstanceVariable as shown below:

11. Save and Run the 'DifferentPackageAccess' class
12. Click on 'Proceed' button on the 'Error in workspace' dialog
13. Observe that output with an error is displayed (i..e. it cant access 'static' instance variable 'a') as shown below:



14.Since we've specified the instance variable 'a' as 'static' and its default access specifier is 'protected' we're not able to access 'static' instance variable 'a'. Lets add 'public' access specifier to the 'static' instance variable 'a' in 'StaticVariablesDemo' class as shown below:



15. Save the 'StaticVariableDemo' and 'DifferentPackageAccess' class files and Run the 'DifferentPackageAccess' class
16. Observe that the output is displayed without any errors in console as shown below:



At this point we've to understand that 'static' instance variables can be accessed without creating an object, by a class which is created in a different package, if and only we've specified the 'static' instance variable as 'public'.

Note - 'static' is one of the non-access specifiers in Java.

Download this project:

Click here to download this project containing the class files used in this post (You can download this project and import into Eclipse IDE on your machine)




Please comment below to feedback or ask questions.

'static' methods will be explained in the next post.



No comments: