The use of the static keyword in a method declaration implies which of the following?

A member in a Java program can be declared as static using the keyword “static” preceding its declaration/definition. When a member is declared static, then it essentially means that the member is shared by all the instances of a class without making copies of per instance.

Thus static is a non-class modifier used in Java and can be applied to the following members:

  • Variables
  • Methods
  • Blocks
  • Classes (more specifically, nested classes)

When a member is declared static, then it can be accessed without using an object. This means that before a class is instantiated, the static member is active and accessible. Unlike other non-static class members that cease to exist when the object of the class goes out of scope, the static member is still obviously active.

Static Variable in Java

A member variable of a class that is declared as static is called the Static Variable. It is also called as the “Class variable”. Once the variable is declared as static, memory is allocated only once and not every time when a class is instantiated. Hence you can access the static variable without a reference to an object.

The following Java program depicts the usage of Static variables:

class Main
{
// static variables a and b
static int a = 10;
static int b;

static void printStatic()
{
    a = a /2;
    b = a;

    System.out.println("printStatic::Value of a : "+a + " Value of b : 
 "+b);
}  

public static void main(String[] args)
{
   printStatic();
   b = a*5;
   a++;

System.out.println("main::Value of a : "+a + " Value of b : "+b);
   }
 }

output::

printStatic::Value of a : Value of b : 5
main::Value of a : 6 Value of b : 25

In the above program, we have two static variables i.e. a and b. We modify these variables in a function “printStatic” as well as in “main”. Note that the values of these static variables are preserved across the functions even when the scope of the function ends. The output shows the values of variables in two functions.

Static Method

A method in Java is static when it is preceded by the keyword “static”.

Some points that you need to remember about the static method include:

  • A static method belongs to the class as against other non-static methods that are invoked using the instance of a class.
  • To invoke a static method, you don’t need a class object.
  • The static data members of the class are accessible to the static method. The static method can even change the values of the static data member.
  • A static method cannot have a reference to ‘this’ or ‘super’ members. Even if a static method tries to refer them, it will be a compiler error.
  • Just like static data, the static method can also call other static methods. A static method cannot refer to non-static data members or variables and cannot call non-static methods too.

The following program shows the implementation of the static method in Java:

class Main
{
  // static method
  static void static_method()
{
    System.out.println("Static method in Java...called without any 
object");
}

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

output:

Static method in Java...called without any object

Static Block In Java

Just as you have function blocks in programming languages like C++, C#, etc. in Java also, there is a special block called “static” block that usually includes a block of code related to static data.

This static block is executed at the moment when the first object of the class is created (precisely at the time of classloading) or when the static member inside the block is used.

The following program shows the usage of a static block.

class Main
{
  static int sum = 0;
  static int val1 = 5;
  static int val2;

// static block
 static {
    sum = val1 + val2;
    System.out.println("In static block, val1: " + val1  + " val2: "+ 
val2 + " sum:" + sum);
    val2 = val1 * 3;
    sum = val1 + val2;
}

 public static void main(String[] args)
{
    System.out.println("In main function, val1: " + val1  + " val2: "+ val2 + " sum:" + sum);
  }
}

output:

In static block, val1: 5 val2: 0 sum:5
In main function, val1: val2: 15 sum:20

Static Class

In Java, you have static blocks, static methods, and even static variables. Hence it’s obvious that you can also have static classes. In Java, it is possible to have a class inside another class and this is called a Nested class. The class that encloses the nested class is called the Outer class.

In Java, although you can declare a nested class as Static it is not possible to have the outer class as Static.

Let’s now explore the static nested classes in Java.

Static Nested Class

As already mentioned, you can have a nested class in Java declared as static. The static nested class differs from the non-static nested class(inner class) in certain aspects as listed below.

Unlike the non-static nested class, the nested static class doesn’t need an outer class reference.

A static nested class can access only static members of the outer class as against the non-static classes that can access static as well as non-static members of the outer class.

An example of a static nested class is given below.


class Main{
  private static String str = "SoftwareTestingHelp";

     //Static nested class
     static class NestedClass{
        //non-static method
            public void display() {

            System.out.println("Static string in OuterClass: " + str);
            }

    }
   public static void main(String args[])
   {
            Main.NestedClassobj = new Main.NestedClass();
            obj.display();
   }
}

output

Static string in OuterClass: SoftwareTestingHelp

I think this is how static keyword works in java.

Which of the following variable is declared as static?

A member variable of a class that is declared as static is called the Static Variable. It is also called as the “Class variable”.

What does the keyword static mean in Java?

What does static mean? When you declare a variable or a method as static, it belongs to the class, rather than a specific instance. This means that only one instance of a static member exists, even if you create multiple objects of the class, or if you don't create any. It will be shared by all objects.

Why is a static variable also referred to as a class variable?

Static variables should be used to track data that are shared in common by all of the instances of a class. These variables are also called class variables. Class variables are distinguished from instance variables. Instance variables track the data that belong to each individual object of a class.

Which of the following is the most correct statement that describes about static variables?

The correct option is C. It can be initialized at compile time itself. It can be changed at runtime. It retains its value during the life of the program, and most importantly a unique value is retained throughout the program.