Thursday, January 21, 2016

Topic 19: Keywords static and final in Java

Understanding static


  • When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. 

  • We can declare both methods and variables to be static. 

  • The most common example of a static member is main( ). main( ) is declared as static because it must be called before any objects exist.

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

  • Methods declared as static have several restrictions:

  • They can only call other static methods.

  • They must only access static data.

  • They cannot refer to this or super in any way.

  • To initialize our static variables, we can declare a static block that gets executed exactly once, when the class is first loaded. 

The following example shows a class that has a static method, some static variables, and a static initialization block:

// Demonstrate static variables, methods, and blocks.
class UseStatic
{
static int a = 3;
static int b;


static void meth(int x)
{
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}

static
{
System.out.println("Static block initialized.");
b = a * 4;
}


public static void main(String args[])
{
meth(42); //No instance object created
}
}

As soon as the UseStatic class is loaded, all of the static statements are run: 

  • First, a is set to 3, then the static block executes. 

  • Then main( ) is called, which calls meth( ), passing 42 to x. 

  • The three println( ) statements refer to the two static variables a and b, as well as to the local variable x.

Output:

Static block initialized.
x = 42
a = 3
b = 12


  • Outside of the class in which they are defined, static methods and variables can be used independently of any object. To do so, we need only specify the name of their class followed by the dot operator. For example: 

    classname.method( ) 

    Here, classname is the name of the class in which the static method is declared. This is how Java implements a controlled version of global methods and global variables. For ex:

StaticDemo.callme(); // To access static method outside class

StaticDemo.b; // To access static variable outside class


Understanding final


A variable can be declared as final. Doing so prevents its contents from being modified. This means, we must initialize a final variable when it is declared. For example:

final int FINALVAR = 1;

Here, FINALVAR will remain constant throughout the program in which it is defined and used.

  •   It is a common coding convention to choose all uppercase identifiers for final variables.  

  • Variables declared as final do not occupy memory on a per-instance basis.

  • The keyword final can also be applied to methods, but its meaning is substantially different than when it is applied to variables. 

 

Click for NEXT article.

 

Please feel free to correct me by commenting your suggestions and feedback.

No comments:

Post a Comment