Construction and Initialization

Constructor automatically new પર run થાય છે અને guarantee કરે છે કે દરેક object valid born થાય છે; parameters સાથે define કરો અને compiler નું free no-arg constructor quietly disappear થઈ જાય છે.

10 min read · 10 cards · 2 checks

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


Theory

book જે broken born થઈ

નવો intern BookBridge માં 20 books add કરે છે. book 14 પર એ title set કરે છે, distracted થાય છે, અને copies ને ક્યારેય set નથી કરતો.

object exist કરે છે. તેનું copies field default, 0 hold કરે છે. અઠવાડિયાં પછી student એને borrow કરવા પ્રયત્ન કરે છે, issue() કહે છે no stock, અને એક physical copy shelf પર એક semester સુધી unissued પડી રહે છે.

કોઈએ wrong value લખી નહોતી; કોઈએ માત્ર forgot કર્યું હતું. fix એ છે કે forgetting ને impossible બનાવવું: કોઈ Book facts state કર્યા વગર create ન થાય.

Theory

admission desk

College student ને "whenever" form fill કરવા માટે wander in કરવા દેતું નથી. Admission desk પર થાય છે: name અને course entry પર લેવામાં આવે છે, અથવા admission નથી.

Constructor એ objects માટે એ desk છે. એ new ના moment પર automatically run થાય છે, facts ને parameters તરીકે receive કરે છે, અને only then object ને hand over કરે છે. કંઈ system માં half-registered enter નથી થતું.

Theory

Constructor, ઔપચારિક રીતે

Constructor એ special member છે જે new object ને initialise કરે છે. Rules જે exam check કરે છે:

  • એનું નામ exactly class name છે
  • એની પાસે કોઈ return type નથી, not even void
  • એ automatically run થાય છે જ્યારે new execute થાય છે; તમે એને method ની જેમ call નથી કરતા
  • એ overloaded થઈ શકે છે: different parameter lists સાથે several constructors

Book(String t, int c) { title = t; copies = c; }

હવે new Book("Let Us C", 5) એક move માં complete book build કરે છે.

Practical

Born complete, 2 ways

class Book {
    String title;
    int copies;

    Book(String t, int c) {     // parameterized constructor
        title = t;
        copies = c;
    }

    Book() {                    // no-arg constructor, written by US
        title = "Untitled";
        copies = 1;
    }
}

public class BookBridge {
    public static void main(String[] args) {
        Book b1 = new Book("Let Us C", 5);
        Book b2 = new Book();   // works because we wrote Book()
        System.out.println(b1.title + " | " + b1.copies);
        System.out.println(b2.title + " | " + b2.copies);
    }
}

Theory

vanishing default constructor

new Book() ક્યાંથી work કરતું હતું, પાછું જ્યારે Book ની પાસે કોઈ constructor નહોતું? compiler એ default constructor supply કર્યો: invisible, no arguments, fields તેમના defaults પર left.

catch: એ gift only ત્યાં સુધી exist કરે છે જ્યાં સુધી તમે તમારો કોઈ constructor define ન કરો. જે moment તમે Book(String t, int c) લખો છો, free one vanish થઈ જાય છે. program ની કોઈપણ જૂની new Book() instantly compile error બની જાય છે, જ્યાં સુધી તમે also no-arg constructor ને yourself ન લખો, જેમ listing કરે છે.

Quiz

class Book { Book(String t) { title = t; } } Elsewhere કોઈ લખે છે: new Book(); શું થાય છે?

  1. Compile error: હવે કોઈ matching constructor exists નથી
  2. એ work કરે છે; compiler નું default constructor હંમેશા available છે
  3. એ work કરે છે; fields just તેમના default values retain કરે છે
  4. Missing constructor ને first call કરવામાં આવે ત્યારે runtime exception
Show the answer

Compile error: હવે કોઈ matching constructor exists નથી

String constructor ને define કરવાથી compiler નું free no-arg one remove થઈ ગયું, એટલે new Book() ને match કરવા કંઈ નથી અને compilation fail થાય છે. Options B અને C એ world ને describe કરે છે BEFORE કોઈ constructor define થયો હતો; એ world તમારા first constructor સાથે end થાય છે. Option D impossible છે: constructor matching entirely compile time પર settle થાય છે. આ vanishing-default rule એ આ paper નો most-asked constructor question છે.

Think first

imposter constructor

student લખે છે: void Book() { title = "X"; } class Book ની અંદર, અને confused છે કે શા માટે new Book() ક્યારેય તેની debug line ને print નથી કરતી. tap કરતા પહેલા: એણે actually શું create કર્યું?

Show the answer

ordinary method જેનું નામ Book છે. moment return type appear થાય છે (even void), એ constructor નથી, એટલે new Book() એને ક્યારેય call નથી કરતું; એ quietly constructor ને instead use કરે છે. તેની method ત્યારે જ run થાય જો કોઈએ b.Book() explicitly લખ્યું હોય, જે કોઈ કરતું નથી. Exam rule: class name + NO return type = constructor; anything else just method છે.

Watch out

અને destructor?

BCA304 reflex: constructor lesson પછી destructor lesson આવે છે. Not in Java. કોઈ destructors નથી; garbage collector unreachable objects ને reclaim કરે છે (comparison lesson નો locked-drawer trade).

જો comparison question પૂછે, તો લખો: C++ constructors ને destructors સાથે pair કરે છે manual cleanup માટે; Java constructors ને automatic garbage collection સાથે pair કરે છે. Java destructor syntax ને invent ન કરો; લખવા માટે કોઈ નથી.

Theory

એક loose thread

આપણા constructor ના parameters જુઓ: Book(String t, int c). શા માટે cryptic t અને c? કારણ કે એને title અને copies name કરવા, natural choice, fields સાથે name clash create કરે છે, અને એ clash ને properly solve કરવા માટે this keyword ની જરૂર છે. એ exactly next lesson છે, જ્યાં BookBridge નું Person family પણ super સાથે parent constructors ને call કરવાનું start કરે છે. admission desk hierarchy મેળવવાનું છે.

Summary

Key takeaways

  • Constructor object ને new પર initialise કરે છે: class name, no return type, ક્યારેય manually called નહીં.
  • Parameterized constructors incomplete objects ને impossible બનાવે છે: facts birth પર arrive થાય છે.
  • compiler નું default constructor only ત્યાં સુધી exist કરે છે જ્યાં સુધી તમે કોઈ constructor define ન કરો; તમારો first one એને remove કરે છે.
  • Constructors overload થાય છે: several parameter lists, one class name.
  • void Book() એ method છે, constructor નહીં: કોઈપણ return type એને disqualify કરે છે.
  • Java માં constructors છે પણ destructors નથી; garbage collection life ના other end ને handle કરે છે.
  • Memory hook: desk વગર કોઈ admission નહીં.

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