Create static variable, method and intializers in java

The staticKeyword

The static keyword declares members (attributes, methods, and nested classes) that are associated with the class rather than the instances of the class.

The following sections describe the most common uses of the static keyword: class variables and class methods.

Class Attributes

Sometimes it is desirable to have a variable that is shared among all instances of a class. For example, you could use this variable as the basis for communication between instances or to keep track of the number of instances that have been created (see Figure 7-1). Figure 7-1

You achieve this shared effect by marking the variable with the keyword static. Such a variable is sometimes called a class variable to distinguish.
it from a member or instance variable, which is not shared.

1 public class Count {

2 private int serialNumber;

3 public static int counter = 0;


5 public Count() {

6 counter++;

7 serialNumber = counter;

8 }

9 }

In this example, every object that is created is assigned a unique serial number, starting at 1 and counting upwards. The variable counter is shared among all instances, so when the constructor of one object increments counter, the next object to be created receives the incremented value.

A static variable is similar in some ways to a global variable in other languages. The Java programming language does not have global variables, but a static variable is a single variable that is accessible from any instance of the class.

If a static variable is not marked as private, you can access it from outside the class. To do this, you do not need an instance of the class, you can refer to it through the class name.

1 public class OtherClass {

2 public void incrementNumber() {

3 Count1.counter++;

4 }

5 }

Class Methods

Sometimes you need to access program code when you do not have an instance of a particular object available. A method that is marked using the keyword static can be used in this way and is sometimes called a class method.

1 public class Count2 {

2 private int serialNumber;

3 private static int counter = 0;


5 public static int getTotalCount() {

6 return counter;

7 }


9 public Count2() {

10 counter++;

11 serialNumber = counter;

12 }

You should access methods that are static using the class name rather than an object reference, as follows:

1 public class TestCounter {

2 public static void main(String[] args) {

3 System.out.println(“Number of counter is ”

4 + Count2.getTotalCount());

5 Count2 count1 = new Count2();

6 System.out.println(“Number of counter is ”

7 + Count2.getTotalCount());

8 }

9 }

The output of the TestCounter program is:

Number of counter is 0

Number of counter is 1

Because you can invoke a static method without any instance of then class to which it belongs, there is no this value. The consequence is that a static method cannot access any variables other than the local variables, static attributes, and its parameters. Attempting to access non-static attributes causes a compiler error.

Non-static attributes are bound to an instance and can be accessed through instance references only.

1 public class Count3 {

2 private int serialNumber;

3 private static int counter = 0;


5 public static int getSerialNumber() {

6 return serialNumber; // COMPILER ERROR!

7 }

You should be aware of the following when using static methods:

● You cannot override a static method but you can hide it. For a method to be overridden it must be non-static. Two static methods with the same signature in a class hierarchy simply means that there are two independent class methods. If a class method is applied to an object reference, the method invoked is the one for the class for which the variable was declared.

● The main() method is a static method because the JVM does not create an instance of the class when executing the main method. So if you have member data, you must create an object to access it.

Static Initializers

A class can contain code in static blocks that are not part of normal methods. Static block code executes once when the class is loaded. If a class contains more than one static block, they are executed in the order of their appearance in the class.

1 public class Count4 {

2 public static int counter;

3 static {

4 counter = Integer.getInteger(“myApp.Count4.counter”).intValue();

5 }

6 }

1 public class TestStaticInit {

2 public static void main(String[] args) {

3 System.out.println(“counter = “+ Count4.counter);

4 }

5 }

The code on Line 4 of the Count4 class uses a static method of the Integer class getInteger(String), which returns an Integer object that represents the value of a system property. This property, named myApp.Count4.counter, is set on the command line using the -D option.

The intValue method on the Integer object returns the value as an int.
The result is the following:

java -DmyApp.Count4.counter=47 TestStaticInit

counter = 47