‘static’ instance variables


AutomationChinnu

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…

View original post 435 more words

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s