Tuesday , January 24 2017
Home / java / Java Static Variable Method Block Class

Java Static Variable Method Block Class

Static:

In Java Static is a keyword, used to define the functionality at class level, hence the static functionality is the class level functionality.

The static can be used in the case of

Variables,

Methods,

Blocks and

Inner Classes.

Static variables :

A variable which is declared by using the static keyword is called as a static variable.

Example : public static int a;

8 Important points about static variable:

  1. Static variables are also called as class variables.
  2. Class variables that are belonging to the entire class, not a specific instance of the class.
  3. Class variables are declared with the static keyword.
  4. A single can be shared by all instances of the class.
  5. We can’t access the static variables from the normal methods.
  6. Class variables are allocated memory only once at the time of class loading. which can be commonly accessed by all the instances of the class.
  7. Static variables are allocated memory the static pool.
  8. Since memory for the class variables is allocated at the time of loading the class itself, we can access the static variables directly with the class name itself.

Static MemoryExample:


/**
 *
 * @author chandrashekhar
 */
class SampleStaticClass {

    static int x;
}

public class StaticVariableDemo {

    public static void main(String[] args) {
        SampleStaticClass.x = 20;
        System.out.println("A.x -- " + SampleStaticClass.x);
        SampleStaticClass object = new SampleStaticClass();
        object.x = SampleStaticClass.x + 20;
        System.out.println("ob.x -- " + object.x);
        SampleStaticClass.x = object.x - 10;
        System.out.println("A.x -- " + SampleStaticClass.x);
        System.out.println("ob.x -- " + object.x);
        SampleStaticClass object2 = new SampleStaticClass();
        object2.x = (object2.x * 2) + (SampleStaticClass.x - 10 + object.x);
        System.out.println("A.x -- " + SampleStaticClass.x);
        System.out.println("ob.x -- " + object.x);
        System.out.println("ob1.x -- " + object2.x);

    }
}

output:

SampleStaticClass.x -- 20
object.x -- 40
SampleStaticClass.x -- 30
object.x -- 30
SampleStaticClass.x -- 110
object.x -- 110
object2.x -- 110

 

Static variables are given default values.

‘0’ in the case of numbers,

‘False’ in the case of Boolean,

‘null’ in the case of characters and other Objects.

Static Methods:

If a method that declared by using the static keyword, then we say that method is a static method. The extreme example for a static method in Java is main method. Static methods are called without using the object. We can directly call the static methods from the main method.

Some important point about static methods:

  1. Static methods can be accessed directly by using class name.
  2. Static methods can’t be overridden.
  3. Non-static methods can access the static methods only by using the class name.
  4. Static methods can access the non-static methods by using the instance of the class.
  5. Both are not accessed directly.
  6. Static method cannot refer to “this” or “super” in any ware.

/**
 *
 * @author chandrashekhar
 */
public class StaticMethodDemo {

    public static void main(String[] args) {
        someStaticMethod();
    }

    public static void someStaticMethod() {
        System.out.println("Hello I am a static method ");
        StaticMethodDemo methodDemo = new StaticMethodDemo();
        methodDemo.sample();
    }

    public void sample() {
        System.out.println("Normal Method ");
        StaticMethodDemo.someOtherStaticMethod();
    }

    public static void someOtherStaticMethod() {
        System.out.println("Hello I am some other static method ");
        StaticMethodDemo methodDemo = new StaticMethodDemo();
        methodDemo.sample();
    }

}

Static block:

Static blocks will be executed at the time of class loading. Hence if we want to perform any operation while loading the class, we can define that functionality with in the static blocks.

Example of Usages:

  1. registering the database drivers,
  2. loading the native libraries.

Syntax :

static{

// Code Here

}

We can define any number of static blocks in a class and all will be executed from the top to bottom.

Some important points about static Block:

  1. Static blocks can be executed by the JVM.
  2. We can not access the static blocks directly or indirectly.
  3. We can not directly access the non static content from the static blocks.
  4. Static blocks can executed before the default constructor. The priority of static block is high compared to default constructor.
  5. We can’t change the access of static blocks (There is no public ,private ,protected static blocks).

/**
 *
 * @author chandrashekhar
 */
public class StaticBlockDemo {

    static {
        System.out.println("static Block 1");
    }

    public StaticBlockDemo() {
        System.out.println("In default Constructor");
    }

    static {
        System.out.println("static Block 2");
    }

    public static void main(String[] args) {
        StaticBlockDemo blockDemo = new StaticBlockDemo();
    }

    static {
        System.out.println("static Block 3");
    }

}

output : 

static Block 1
static Block 2
static Block 3
In default Constructor

 

Can we write a Java program with out main method ?

Yes. We can write a program with out using the main method, but this is possible before versions of Java 1.7. In Java 1.7, JRE looks for a main method before going to load the class.If JRE doesn’t find the main it will throw the bellow error.


Main method not found in class StaticBlockDemo, please define the main method as:
public static void main(String[] args)

In before versions of Java 1.7 JRE looks for the main method after executing the static blocks. But the execution and priority of the static blocks are fixed.

Static Class :

We can create a static class in Java, In Java we can create a class with the another class those classes are called nested or inner classes.

Inner classes : A class defined under the another class is called inner class or nested class. An inner class may be static or non-static. There is some functionality difference between these two types of classes, we will discuss this topic on next tutorials.

Example :


/**
*
* @author chandrashekhar
*/
public class InnerClassDemo {

public static void main(String[] args) {

// Accessing the Static Inner class
InnerClassDemo.StaticInnerClass innerClass = new StaticInnerClass();
System.out.println("StaticInnerClass value  : " + innerClass.getValue());

// Accesing the Normal Inner class
InnerClassDemo.NormalInnerCalss normalInnerCalss = new InnerClassDemo().new NormalInnerCalss();
System.out.println("normalInnerCalss Value : " + normalInnerCalss.getValue());

}

static class StaticInnerClass {

int a = 10;

public int getValue() {
return a;
}
}

class NormalInnerCalss {

int instVar = 20;

public int getValue() {

return instVar;
}
}

}

Output :

StaticInnerClass value  : 10
NormalInnerCalss Value : 20

 

About chandrashekhar

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

Check Also

Type Inference in Java 7 Example

Type Inference in Java 7 is another great addition introduced to ease the developer to type …

Leave a Reply

Your email address will not be published. Required fields are marked *