Static members, static block, static class

static members class खुद की होती हैं: एक shared totalBooks counter, objects के बिना callable methods, class load पर एक बार run होने वाला एक block, और nested-only static class rule।

11 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

एक Number जो किसी की नहीं है

Librarian dashboard पर एक line चाहती है:

Total books in library: 6000

वह number कहाँ रहती है? किसी single Book के अंदर नहीं: हर object अपना खुद का title और copies जानता है, और कोई book अपने 5999 colleagues को count करने के लिए responsible नहीं है। Total पूरे collection का है, यानी, class का है।

Java के पास exactly ऐसे data के लिए एक home है: static members, वे जो "class के हैं, किसी object के नहीं" marked हैं।

Theory

Diaries और Notice Board

एक hostel का हर member अपनी personal diary रखता है: आपकी entries सिर्फ़ आपकी हैं। यह एक instance field है: एक object एक copy।

Hostel में corridor में एक notice board भी है: एक single copy, हर कोई same एक पढ़ता और update करता है। यह एक static field है। Board पर "total residents: 94" pin कीजिए, और यह किसका number है इसमें कोई confusion नहीं है: यह hostel का है।

Theory

Static Family, Formally

Static field: पूरी class के लिए एक copy, हर object से shared। static int totalBooks;

Static method: class पर खुद call किया जाता है, Book.resetCatalog(), कोई object नहीं चाहिए। इसके अंदर कोई this नहीं है, तो यह instance fields को directly touch नहीं कर सकता।

Static block: static { ... } exactly एक बार run होता है, जब JVM पहली बार class load करता है, किसी object या main के पहले use से पहले।

Static class: सिर्फ़ एक class जो किसी दूसरे के अंदर nested है वह static हो सकती है; एक top-level static class compile नहीं होती।

Practical

Shared Counter

class Book {
    static int totalBooks = 0;   // ONE copy, on the notice board
    String title;                // per-object, in each diary

    static {                     // runs once, at class load
        System.out.println("Book catalog initialised");
    }

    Book(String t) {
        title = t;
        totalBooks++;            // every birth ticks the shared counter
    }
}

public class BookBridge {
    public static void main(String[] args) {
        new Book("Let Us C");
        new Book("Java Complete Reference");
        new Book("Data Structures");
        System.out.println("Total books: " + Book.totalBooks);
    }
}

Think first

Counter Trace कीजिए

Listing को अपने दिमाग़ में line by line run कीजिए। Tap करने से पहले: exactly क्या print होता है, किस order में, और पहली line बिल्कुल क्यों appear होती है?

Show the answer

पहले Book catalog initialised: static block class load होने पर fire होता है, पहले new Book से triggered, किसी constructor body से पहले। फिर 3 constructors हर एक SAME shared field पर totalBooks++ run करते हैं: 0 से 1 से 2 से 3। आख़िर में Total books: 3। अगर totalBooks एक instance field होती, हर book privately 1 count करती और कहीं भी कोई total exist नहीं करता: वह one-shared-copy behaviour ही static का पूरा point है।

Quiz

एक static block कितनी बार execute होता है?

  1. हर create किए गए object के लिए एक बार
  2. एक बार, जब class पहली बार load होती है
  3. हर बार जब एक static method call होता है
  4. कभी नहीं, जब तक explicitly call न किया जाए
Show the answer

एक बार, जब class पहली बार load होती है

Static block class का है, तो यह class-loading time पर run होता है, program run में exactly एक बार, चाहे आप फिर 6000 Books create करें या load के बाद कोई न करें। Option A एक constructor की rhythm describe करता है, classic confusion। Options C और D triggers invent करते हैं जो exist नहीं करते: कोई static block को call नहीं करता; JVM इसे unprompted run करता है। Exam phrasing याद रखने के लिए: constructor = per object एक बार, static block = per class एक बार।

Watch out

Rules जो static तोड़ता है अगर आप उसे let करें

कोई this नहीं, कोई instance fields नहीं: एक static method के अंदर कोई current object नहीं है, तो this और title का direct use compile errors हैं। यही exactly वजह है पिछले lesson ने कहा this main में appear नहीं हो सकता।

Top-Level Static Class: file level पर static class Book compile नहीं होता; static सिर्फ़ nested classes पर apply होता है।

b1.totalBooks compile होता है पर एक instance field की तरह पढ़ता है और readers को mislead करता है: हमेशा Book.totalBooks लिखिए।

Theory

आप पूरे समय static इस्तेमाल कर चुके हैं

public static void main: static ताकि JVM program को पहले कोई object construct किए बिना start कर सके; आपकी बहुत पहली listing से वह mysterious word अब एक meaning रखता है। Math.sqrt(16), Integer.parseInt("42"): static methods, classes पर call किए गए, कहीं भी new नहीं। और यहाँ का pattern, constructor में ticked एक shared counter, एक standard exam program है: इसे एक blank page से लिखने की practice कीजिए।

Summary

Key takeaways

  • static = class की है: एक shared copy, per object एक नहीं।
  • Static methods बिना object के run होते हैं; इनके अंदर कोई this नहीं, कोई direct instance access नहीं।
  • एक static block exactly एक बार run होता है, class load पर; constructors per object एक बार run होते हैं।
  • सिर्फ़ nested classes static हो सकती हैं; top-level static classes compile नहीं होतीं।
  • ClassName.member की तरह access कीजिए: Book.totalBooks, Math.sqrt।
  • main static है ताकि JVM को शुरू करने के लिए कोई object न चाहिए।
  • Memory hook: per object एक diary, per class एक notice board।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Classes and Objects

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Static members, static block, static class · Java Programming Language · Gri-Learn