Skip to main content

Understanding the static Keyword in Java


What is the static Keyword?

In Java, the static keyword is used to indicate that a particular member (variable or method) belongs to the class itself, rather than to instances of the class. This means that the static member can be accessed without creating an instance of the class.

Static Variables

A static variable is shared among all instances of a class. It is also known as a class variable.

Example:

public class Example { // static variable static int count = 0; Example() { count++; } public static void main(String[] args) { Example obj1 = new Example(); Example obj2 = new Example(); Example obj3 = new Example(); System.out.println("Count: " + Example.count); // Output: Count: 3 } }

In this example, the count variable is static and shared among all instances of the Example class. It keeps track of the number of instances created.

Static Methods

A static method belongs to the class rather than any particular object instance. You can call a static method without creating an object of the class.

Example:

public class Example { // static method static void displayMessage() { System.out.println("Hello, World!"); } public static void main(String[] args) { Example.displayMessage(); // Output: Hello, World! } }

In this example, the displayMessage method is static, so it can be called directly using the class name without creating an instance of the Example class.

Static Block

A static block is used for static initialization of a class. This block is executed when the class is first loaded into the memory.

Example:

public class Example { static int num; static String str; // static block static { num = 100; str = "Static Block Initialization"; } public static void main(String[] args) { System.out.println("Number: " + Example.num); // Output: Number: 100 System.out.println("String: " + Example.str); // Output: String: Static Block Initialization } }

In this example, the static block initializes the static variables num and str when the class is loaded.

Static Nested Class

A static nested class is a static class defined within another class. It can access the static members of the outer class.

public class OuterClass { static int data = 30; static class InnerClass { void display() { System.out.println("Data: " + data); } } public static void main(String[] args) { OuterClass.InnerClass inner = new OuterClass.InnerClass(); inner.display(); // Output: Data: 30 } }

In this example, InnerClass is a static nested class within OuterClass, and it can access the static variable data of the outer class.

Key Points to Remember

  1. Static variables and methods belong to the class and can be accessed without creating an instance of the class.
  2. Static methods can only directly access other static members (variables and methods).
  3. Static blocks are executed when the class is loaded and are used for static initialization.
  4. Static nested classes can access the static members of the outer class.
  5. The static keyword cannot be used with local variables inside methods

Comments

Popular posts from this blog

OSPF Configuration

Configuring OSPF  Step 1: Enter Configuration Mode Router>   enable Router#  configure terminal Step 2: Enable OSPF Enable OSPF with a process ID. The process ID is locally significant and can be any number. Router(config)# router ospf <process-Id> Step 3: Set the OSPF Router ID Setting a unique OSPF Router ID is optional but recommended for stability and troubleshooting. The Router ID should be a unique IPv4 address within the OSPF domain. Router(config-router)# router-id <router-id> Step 4: Define Networks to Include in OSPF Specify which networks will participate in OSPF and their corresponding areas. Ensure the areas match across all routers in the OSPF domain. Router(config-router)# network <network-address> <wildcard-mask> area <area-id> Step 5: Save the configuration Router(config-router)# exit Router(config)# do write / do wr Verification Commands View OSPF Neighbors show ospf neighbor View the OSPF Routing Table show ip route ospf View OSPF Pro

Java Overview

What is Java?  Java is a popular programming language for developing web, mobile, and desktop applications. It is a high-level and object-oriented programming language. It is platform-independent. It required the Java Virtual Machine(JVM) to run the Java code. Java is similar to C/C++. Application of Java Mobile Application Desktop Application Web Application Embedded System Smart Card Payment System Games History of Java Java's history dates back to the early 1990s when a group of researchers at Sun Microsystems led by James Gosling started a "green project". The goal of this project was to create a portable home-appliance software that could run on embedded processor chips. Initially, the team planned to use C++ programming language for this project. However, they encountered portability issues with C++ and decided to develop a new programming language from scratch. This led to the creation of the Oak programming language, named after an oak tree outside Gosling's o

Configuring Static NAT for Any Network Scenario

Introduction Network Address Translation (NAT) is a fundamental technique used in networking to translate private IP addresses to public IP addresses and vice versa. Static NAT is a method where a specific private IP address is mapped to a specific public IP address.  Configuration Steps Router Configuration Enable NAT Service Router(config)# ip nat inside source static [inside-local] [inside-global] Replace [inside-local] with the private IP address to be translated. Replace [inside-global] with the public IP address to translate to. Configure Interfaces Router(config)# interface [inside-interface] Router(config-if)# ip address [inside-ip-address] [subnet-mask] Router(config-if)# ip nat inside Router(config-if)# no shutdown Router(config-if)# exit Router(config)# interface [outside-interface] Router(config-if)# ip address [outside-ip-address] [subnet-mask] Router(config-if)# ip nat outside Router(config-if)# no shutdown Router(config-if)# exit Replace [inside-interface]