Static members, static block, static class

static members class itself ને belong કરે છે: એક shared totalBooks counter, methods જે objects વગર callable છે, block જે class load પર once run થાય છે, અને nested-only static class rule.

11 min read · 9 cards · 2 checks

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


Theory

number જે કોઈને belong નથી કરતું

librarian ને dashboard પર એક line જોઈએ છે:

Total books in library: 6000

એ number ક્યાં lives કરે છે? કોઈ single Book ની અંદર નહીં: દરેક object તેનું own title અને copies જાણે છે, અને કોઈ book તેના 5999 colleagues ને count કરવા માટે responsible નથી. total એ collection as a whole ને belong કરે છે, એટલે કે class ને.

Java ની પાસે exactly એવા data માટે home છે: static members, જે "belongs to the class, not to any object" તરીકે marked છે.

Theory

Diaries અને notice board

hostel નો દરેક member personal diary રાખે છે: તમારી entries તમારી alone છે. એ instance field છે: એક copy per object.

hostel ની પાસે corridor માં એક notice board પણ છે: એક single copy, દરેક same one ને read અને update કરે છે. એ static field છે. board પર "total residents: 94" pin કરો, અને કોઈ confusion નથી કે એ કોનું number છે: એ hostel નું છે.

Theory

static family, ઔપચારિક રીતે

Static field: આખી class માટે એક copy, દરેક object દ્વારા shared. static int totalBooks;

Static method: class itself પર called, Book.resetCatalog(), કોઈ object needed નથી. તેની અંદર કોઈ this નથી, એટલે તે instance fields ને directly touch નથી કરી શકતું.

Static block: static { ... } exactly once run થાય છે, જ્યારે JVM first class ને load કરે છે, કોઈ object અથવા even main ના first use પહેલાં.

Static class: ફક્ત class nested inside another static હોઈ શકે છે; top-level static class compile નથી થતું.

Practical

shared counter

class Book {
    static int totalBooks = 0;   // ONE copy, notice board પર
    String title;                // per-object, દરેક diary માં

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

    Book(String t) {
        title = t;
        totalBooks++;            // દરેક birth shared counter ને tick કરે છે
    }
}

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 ને તમારા mind માં run કરો, line by line. tap કરતા પહેલા: exactly શું print થાય છે, કયા order માં, અને first line શા માટે appear થાય છે?

Show the answer

પહેલા Book catalog initialised: static block fire થાય છે જ્યારે class load થાય છે, first 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 નો whole point છે.

Quiz

static block કેટલી વાર execute થાય છે?

  1. દરેક object created માટે once
  2. Once, જ્યારે class first load થાય છે
  3. દરેક વાર જ્યારે static method called થાય છે
  4. Never, જ્યાં સુધી explicitly called ન થાય
Show the answer

Once, જ્યારે class first load થાય છે

static block class ને belong કરે છે, એટલે તે class-loading time પર run થાય છે, exactly once per program run, even if તમે પછી 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 = once per object, static block = once per class.

Watch out

rules જે static break કરે છે જો તમે let કરો

No this, no instance fields: static method ની અંદર કોઈ current object નથી, એટલે this અને title નો direct use compile errors છે. આ exactly છે કારણ કે last lesson એ કહ્યું કે this main માં appear નથી થઈ શકતું.

Top-level static class: file level પર static class Book compile નથી થતું; static ફક્ત nested classes પર apply થાય છે.

b1.totalBooks compiles પણ instance field જેવું reads કરે છે અને readers ને mislead કરે છે: હંમેશા Book.totalBooks લખો.

Theory

તમે static ને all along use કર્યું છે

public static void main: static એટલે JVM program ને કોઈ object construct કર્યા વગર first start કરી શકે છે; તમારા very first listing નો એ mysterious word હવે meaning ધરાવે છે. Math.sqrt(16), Integer.parseInt("42"): static methods, classes પર called, ક્યાંય new નથી. અને અહીં pattern, constructor માં ticked એક shared counter, એ standard exam program છે: blank page થી એને write કરવાનો practise કરો.

Summary

Key takeaways

  • static = class ને belong કરે છે: one shared copy, not one per object.
  • Static methods object વગર run થાય છે; તેમની અંદર no this, no direct instance access.
  • Static block exactly once run થાય છે, class load પર; constructors દરેક object માટે once run થાય છે.
  • ફક્ત nested classes static હોઈ શકે છે; top-level static classes compile નથી થતા.
  • ClassName.member તરીકે access કરો: Book.totalBooks, Math.sqrt.
  • main static છે એટલે JVM ને begin કરવા માટે કોઈ object ની જરૂર નથી.
  • Memory hook: diary per object, one notice board per class.

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