Friday, May 23, 2025
What is Static Keyword in Java

Introduction to Static Keyword in Java
Java में, static keyword का use मुख्य रूप से memory management के लिए किया जाता है। इसका use variables, methods, blocks and nested classes के साथ किया जा सकता है। यह एक keyword है जो किसी दिए गए class के समान variable or method को share करने के लिए use किया जाता है। Basically, static का use constant variable या ऐसी method के लिए किया जाता है जो किसी class के प्रत्येक example के लिए समान हो। एक class की main method आम तौर पर static labeled की जाती है।

Static member बनाने के लिए, आपको keyword static के साथ इसको पहले declair करना होगा। जब class के किसी member को static declared किया जाता है, तो इसकी class की objects के बनने से पहले और बिना किसी object reference के इसे access किया जा सकता है।

Applications of Static keyword

  • Static Block
  • Static Variable
  • Static Method
  • Static Classes

1. Static Block
यदि आपको अपने static variables को आरंभ करने के लिए computation करने की आवश्यकता है, तो आप एक static block को declair कर सकते हैं जो class में पहली बार load होने पर ठीक एक बार execute हो जाता है। Static Block के use को समझने के लिए नीचे दिए गए Java program पर एक नज़र डालें।

import java.util.*;
public class BlockExample{
static int a = 20;
static int b;

static {
System.out.println(“Static block initialized.”);
b = a * 9;
}

public static void main(String[] args)
{
System.out.println(“Inside main method”);
System.out.println(“Value of a : “+a);
System.out.println(“Value of b : “+b);
}
}

2. Static Variable
जब आप किसी variable को static declare करते हैं, तो variable की एक copy को class level पर सभी objects के बीच created and divided किया जाता है। Static variables are, essentially, global variables हैं। Basically, class के सभी example same static variable share करते हैं। Static variables केवल class-level पर बनाए जा सकते हैं।

import java.util.*;

public class VariableExample
{
static int a = b();

static {
System.out.println(“Inside the static block”);
}

static int b() {
System.out.println(“from b “);
return 20;
}

public static void main(String[] args)
{
System.out.println(“Value of a : “+a);
System.out.println(“Inside main method”);
}
}

3. Static Methods
जब कोई method static keyword के साथ declare की जाती है, तो इसे एक static method के रूप में जाना जाता है। static method का सबसे आम example main( ) method है। static के रूप में declared तरीके following restrictions हो सकते हैं:

वे सीधे अन्य static methods को ही call कर सकते हैं।

वे static data को directly access कर सकते हैं।

public class StaticMethodExample
{
static int j = 100;

int n = 200;

static void a()
{
a = 200;
System.out.println(“Print from a”);

n = 100;

a2();

System.out.println(super.j);
}

void a2()
{
System.out.println(“Inside a2”);
}

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

4. Static Classes
एक class को तभी static बनाया जा सकता है जब वह एक nested class हो। Nested static class को Outer class के reference की need नहीं है। इस स्थिति में, एक static class Outer class के non-static members तक नहीं पहुँच सकता है। आइए एक example लें कि यह कैसे work करता है|

public class NestedExample{
private static String str= “Codeash”
static class MyNestedClass{
public void disp(){
System.out.println(str);
}
}
public static void main(String args[]){
NestedExample.MyNestedClass obj = new NestedExample.MyNestedClass();
obj.disp();

}

My name is Yash Pogra and I am the chief blogger at Codeash and where I like to share my internet/tech experience with my online readers on this website. I have been a webmaster from 2015 which is when I had registered my first company by the name Codeash. I have ventured into different online businesses like offering SEO Services, Digital Marketing and website development services.

Related Article

No Related Article

0 Comments

Leave a Comment