AI May 15, 2023 5 min read

Module 24: Static Keyword

BR

Bhargava Ram Pantina

Web Developer & AI Enthusiast

⚙️ What is the static Keyword?

The static keyword in Java is used for memory management. It can be applied to variables, methods, blocks, and nested classes.

  • Static Variable: Shared among all objects of the class.
  • Static Method: Can be called without creating an object.
  • Static Block: Runs once when the class is loaded.

📘 Examples

1. Static Variable

                    class Student {
                      int id;
                      String name;
                      static String college = "ABC College";  // shared by all instances
                    
                      Student(int i, String n) {
                        id = i;
                        name = n;
                      }
                    
                      void display() {
                        System.out.println(id + " " + name + " " + college);
                      }
                    }

2. Static Method

                    class MathUtil {
                      static int square(int x) {
                        return x * x;
                      }
                    }
                    
                    public class Main {
                      public static void main(String[] args) {
                        System.out.println(MathUtil.square(5));  // Outputs: 25
                      }
                    }
                    

3. Static Block

                    class Demo {
                      static {
                        System.out.println("Static block executed");
                      }
                    
                      public static void main(String[] args) {
                        System.out.println("Main method");
                      }
                    }
                    // Output:
                    // Static block executed
                    // Main method
                    
Artificial Intelligence Web Development Technology Trends Machine Learning UX Design

Share this article

About the Author

BR

Bhargava Ram Pantina

Bhargava Ram is a web developer and AI enthusiast with over 8 years of experience in the tech industry. He specializes in creating innovative web solutions that leverage the latest technologies to solve real-world problems.

Related Articles

AI Mar 15, 2023

Implementing ChatGPT in Your Web Application: A Step-by-Step Tutorial

Learn how to integrate OpenAI's ChatGPT into your web application to create intelligent chatbots and enhance user interactions.

Read More →
Web Development Apr 22, 2023

Building Accessible Web Forms: A Complete Guide

Learn how to create web forms that are accessible to all users, including those with disabilities. This comprehensive guide covers ARIA attributes, keyboard navigation, and more.

Read More →

Subscribe to My Newsletter

Get the latest articles and resources delivered straight to your inbox.

No spam, just valuable content. Unsubscribe anytime.