• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Jira Selenium Protractor Testing SDLC Agile Methodology

Java Selenium Tutorial & Testing Interview Questions

  • Home
  • Interview Questions
  • Java
  • Java Programs
  • Test Cases
  • Selenium
  • Manual Testing
  • Difference
  • Search
SoftwareTestingo » Java » Java Tutorial » Static keyword in Java With Examples in Java Programming Langauge

Static keyword in Java With Examples in Java Programming Langauge

Last Updated on: October 20, 2019 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Static Variables
  • Static Variable Example
  • Static Method
  • Static Blocks
  • Static Nested Classes
  • How Static Block Works In Java With Example Updated?
  • How to Static method works in Java With Example?
  • What is Data hiding in Java Program With Example?

In the previous article, we have discussed the do-while loop, which is a continuation post of core Java tutorial post, we are going to learn about the static keyword in java and the uses of that. Because In Java programming language static keyword is used regularly.

We are going to discuss about:

  • Static Variable
  • Static Method
  • Static Block
  • Static Nested Class

The Static keyword in Java used for memory management. We can use the static keyword with class, variable, methods and block. If a class have static members, then that belongs to the classes instead of specific instances. That means if you make a member static, then we can invoke those members without creating the objects.

Before going deep let’s take a simple example for better understand how the static keyword works.

class SimpleStaticEx
{
   // This is a static method
   static void myDemoMethod()
   {
      System.out.println("Method Executed");
   }

   public static void main(String[] args)
   {
      /* We are calling a static method without creating an object */
      myDemoMethod();
   }
}

We have seen in the above example, the myDemoMethod is a static method, and we are calling that method without creating an object. That means when we are creating a static member, then that is created at the class level. If we are removing the static keyword then for access that member we have to create an instance of that class otherwise we can’t access that.

Static Variables

When you have declared a variable with the keyword static, then a single copy of that variable is created and shared among all objects or instances of the class. Mostly when we are declaring some global variables, we define those variables with the static keyword. So that all instances of those classes can share the same static variable.

Note:

  • We can create a static variable at a class level only.
  • The execution order of Static variable and the static block is the same the order as they are present in the program.

Let’s take an example to understand how the static variable and static block are executed.

Static Variable Example

package java_Basics;

public class StaticVariable_Example 
{
   static int count=0;
   public void increment()
   {
      count++;
   }
   @SuppressWarnings("static-access")
   public static void main(String[] args) 
   {
      StaticVariable_Example obj1=new StaticVariable_Example();
      StaticVariable_Example obj2=new StaticVariable_Example();
      obj1.increment();
      obj2.increment();
      System.out.println("Obj1: count is="+obj1.count);
      System.out.println("Obj2: count is="+obj2.count);
   }
}

Output:

Obj1: count is=2
Obj2: count is=2

Static Method

When we define a method with the static keyword, that method is called a static method. The Common static method which we regularly use when we are writing in any java program is the main() method. As we discussed above, we can access any static member before creating any objects of that class and without reference to any object that’s, why main() is executed without creating an object of that class.

The static method has several restrictions like:

  • They can directly call other static methods and static members.
  • Can not refer to this or super variable or methods.

When do you need to use Static variables and methods?

You should use the static variables to those properties which are common for all. For example, in an organisation, all employees share the same organisation name. Similarly, for change the static variables value, we need static methods.

package java_Basics;

public class StaticMethod_Example 
{
   static int var = 10;
   static String str = "SoftwareTestingo.com";
   //This is a static method
   public static void main(String[] args) 
   {
      System.out.println("Variable Value: "+var);
      System.out.println("String Value: "+str);
   }
}

Output:

Variable Value: 10
String Value: SoftwareTestingo.com

Static Blocks

Static blocks are mainly used to initialise the static variables. This static block gets executed only once at the time of the class is loaded into the memory.

For Better understanding refer to the below program:

package java_Basics;
public class StaticBlock_Example 
{
   static int num;
   static String mystr;
   static
   {
      num = 100;
      mystr = "Static String Varibale Initilized inside static block";
   }
   public static void main(String[] args) 
   {
      System.out.println("Value of num: "+num);
      System.out.println("Value of mystr: "+mystr);
   }
}

Output:

Value of num: 100
Value of mystr: Static String Varibale Initilized inside static block

Static Nested Classes

We can not declare the top-level classes with static modifiers, but we can declare the inner classes with static modifiers. Such types of classes called Nested static classes.

How Static Block Works In Java With Example Updated?

package com.java.Softwaretestingblog;
class Staticsimple1
{
   static
   {
      System.out.println("Staticsimple1 Static Block");
   }
   Staticsimple1()
   {
      System.out.println("Staticsimple1 constructor Called");
   }
}
class Staticsimple2 extends Staticsimple1
{

   static
   {
      System.out.println("Staticsimple2 Static Block");
   }
   Staticsimple2()
   {
      super();
      System.out.println("Staticsimple2 Constructor Called");
   }
}
public class StaticBlockExample {
   static
   {
      System.out.println("Main Method Static Block");
   }
   public static void main(String[] args) {
      // TODO Auto-generated method stub
      @SuppressWarnings("unused")
      StaticBlockExample obj=new StaticBlockExample();
      @SuppressWarnings("unused")
      Staticsimple2 obj1=new Staticsimple2();
      @SuppressWarnings("unused")
      Staticsimple1 obj2=new Staticsimple1();
   }
}
Check Also: Declare and Implement Interface in java

Output:

Main Method Static Block
Staticsimple1 Static Block
Staticsimple2 Static Block
Staticsimple1 constructor Called
Staticsimple2 Constructor Called
Staticsimple1 constructor Called

How to Static method works in Java With Example?

package com.java.Softwaretestingblog;
class StaticComponents
{
   static int staticVariable;
   static
   {
      System.out.println("StaticComponents SIB");
      staticVariable = 10;
   }
   static void staticMethod()
   {
      System.out.println("From StaticMethod");
      System.out.println(staticVariable);
   }
}
public class MainClassExample {
   //First MainClass Static Method Executed Later Above Class Static Method Executed
   {
      System.out.println("MainClass SIB");
   }
   public static void main(String[] args) 
   {
      //Static Members directly accessed with Class Name
      StaticComponents.staticVariable = 20;
      StaticComponents.staticMethod();
   }
}
Check Also: Static Variable Works In Java

Output:

StaticComponents SIB
From StaticMethod
20

What is Data hiding in Java Program With Example?

package com.java.Static;
class base
{
   static void test()
   {
      System.out.println("Base Class Executed");
   }
}
public class DataHiding extends base {
   static void test()
   {
      System.out.println("Sub Class Executed");
   }
   public static void main(String[] args) 
   {
      /*@SuppressWarnings("unused")
      base obj=new base();
      base.test();
      @SuppressWarnings("unused")
      DataHiding obj1=new DataHiding();
      DataHiding.test();*/
      @SuppressWarnings("unused")
      base obj2=new DataHiding();
      base.test(); //Here DataHiding Happens Parent Class Method Called
   }
}

Ref: article

    Filed Under: Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

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

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Tutorials Important Links

    • Software Testing Jobs
    • Manual Testing Tutorial
    • Selenium Tutorial
    • Core Java Tutorial
    • TestNG Tutorial
    • Java Programs
    • Selenium Programs
    • Manual Test Cases
    • Interview Tips
    • Contact US
    • www.softwaretestingo.com

    Important Links

    • Software Testing Interview Questions
    • Agile Interview Questions
    • Manual Testing Interview Questions
    • Testing Interview Questions
    • Selenium Interview Questions
    • Selenium Real Time Interview Questions
    • Selenium WebDriver Interview Questions
    • Automation Framework Interview Questions
    • Appium Interview Questions
    • TestNG Interview Questions
    • Java Interview Questions
    • Core Java Interview Questions

    Categories

    Copyright © 2021 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy