נבנה על ידיBuilt by Ariel Yohimovich

חזרה למפגשBack
JavaJava

בנאים ו-thisConstructors & this

מפגש 14: בנאים, overloading של בנאים, this() ו-this.field, וסדר האתחול.Session 14: Constructors, constructor overloading, this() and this.field, and initialization order.

מה נעשה היוםToday's plan

  • מונחיםTerms
  • בנאי: אתחול בשורה אחתA constructor: setup in one line
  • כמה בנאים ו-this(...)Several constructors and this(...)
  • הבנאי הריק נעלםThe empty constructor disappears
  • סדר האתחולInitialization order
  • טעויות נפוצותCommon mistakes
נושאTopic 1

מונחיםTerms

המונחים של המפגשThis session's terms

  • בנאי: "מתודה" מיוחדת בשם המחלקה, בלי טיפוס החזרה, שרצה ב-new:constructor: A special "method" named after the class, with no return type, that runs on new:
  • this: הפניה לעצם הנוכחי.this: A reference to the current object.
  • הסתרה: כשלפרמטר או למשתנה מקומי יש שם של שדה, הוא "מסתיר" את השדה.shadowing: When a parameter or local variable has a field's name, it "hides" the field.
  • שרשור בנאים: this(...) בשורה הראשונה של בנאי קורא לבנאי אחר של אותה מחלקה.constructor chaining: this(...) on a constructor's first line calls another constructor of the same class.
  • בנאי ברירת מחדל: אם לא כתבתם שום בנאי, Java נותנת בנאי ריק בלי פרמטרים.default constructor: If you wrote no constructor, Java gives an empty one with no parameters.
  • סדר האתחול: ב-new:initialization order: On new:
נושאTopic 2

בנאי: אתחול בשורה אחתA constructor: setup in one line

בנאי: אתחול בשורה אחתA constructor: setup in one line

  • בנאי רץ ב-new ומקבל ערכים.A constructor runs on new and takes values.
  • this.title הוא השדה, title הוא הפרמטר.this.title is the field, title is the parameter.
  • בזיכרון רואים את המסגרת new Book עם this שמצביע על העצם החדש.In memory you see the new Book frame with this pointing at the new object.
נושאTopic 3

כמה בנאים ו-this(...)Several constructors and this(...)

כמה בנאים ו-this(...)Several constructors and this(...)

  • בנאי קצר מעביר את העבודה לבנאי המלא עם this(title, "unknown").A short constructor hands the work to the full one with this(title, "unknown").
  • כך האתחול כתוב במקום אחד.That way the setup lives in one place.
נושאTopic 4

הבנאי הריק נעלםThe empty constructor disappears

הבנאי הריק נעלםThe empty constructor disappears

  • ברגע שכתבתם Book(String title), Java כבר לא נותנת Book().As soon as you write Book(String title), Java no longer gives you Book().
  • javac עוצר ומסביר מה נדרש ומה נמצא.javac stops and explains what's required and what was found.
נושאTopic 5

סדר האתחולInitialization order

סדר האתחולInitialization order

  • כשהבנאי מתחיל, a כבר 5 (האתחול שליד השדה רץ קודם), ו-b עדיין 0.When the constructor starts, a is already 5 (the initializer next to the field ran first), and b is still 0.
  • רק אחר כך הבנאי קובע את b.Only then does the constructor set b.
נושאTopic 6

טעויות נפוצותCommon mistakes

טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java says

  • constructor Book in class Book cannot be applied to given types;:constructor Book in class Book cannot be applied to given types;:
נושאTopic 7

פרויקט מדףShelf project

הצעד של היום בפרויקט מדףToday's step in the Shelf project

  • מדף, צעד 14: בנאים ל-BookShelf, step 14: Book constructors

בנאי: "מתודה" מיוחדת בשם המחלקה, בלי טיפוס החזרה, שרצה ב-new:constructor: A special "method" named after the class, with no return type, that runs on new:

this: הפניה לעצם הנוכחי.this: A reference to the current object.

הסתרה: כשלפרמטר או למשתנה מקומי יש שם של שדה, הוא "מסתיר" את השדה.shadowing: When a parameter or local variable has a field's name, it "hides" the field.

שרשור בנאים: this(...) בשורה הראשונה של בנאי קורא לבנאי אחר של אותה מחלקה.constructor chaining: this(...) on a constructor's first line calls another constructor of the same class.

בנאי ברירת מחדל: אם לא כתבתם שום בנאי, Java נותנת בנאי ריק בלי פרמטרים.default constructor: If you wrote no constructor, Java gives an empty one with no parameters.

סדר האתחול: ב-new:initialization order: On new:

בנאי רץ ב-new ומקבל ערכים.A constructor runs on new and takes values.

this.title הוא השדה, title הוא הפרמטר.this.title is the field, title is the parameter.

בזיכרון רואים את המסגרת new Book עם this שמצביע על העצם החדש.In memory you see the new Book frame with this pointing at the new object.

בנאי קצר מעביר את העבודה לבנאי המלא עם this(title, "unknown").A short constructor hands the work to the full one with this(title, "unknown").

כך האתחול כתוב במקום אחד.That way the setup lives in one place.

ברגע שכתבתם Book(String title), Java כבר לא נותנת Book().As soon as you write Book(String title), Java no longer gives you Book().

javac עוצר ומסביר מה נדרש ומה נמצא.javac stops and explains what's required and what was found.

כשהבנאי מתחיל, a כבר 5 (האתחול שליד השדה רץ קודם), ו-b עדיין 0.When the constructor starts, a is already 5 (the initializer next to the field ran first), and b is still 0.

רק אחר כך הבנאי קובע את b.Only then does the constructor set b.

constructor Book in class Book cannot be applied to given types;:constructor Book in class Book cannot be applied to given types;:

מדף, צעד 14: בנאים ל-BookShelf, step 14: Book constructors

מה חסר בכותרת של בנאי, לעומת מתודה רגילה?What's missing from a constructor's header, compared to a normal method?

  • טיפוס החזרהA return type
  • שםA name
  • סוגרייםBrackets
  • פרמטריםParameters

טיפוס החזרהA return type

בבנאי Book(String title) { title = title; }, מה הערך של השדה title אחרי new Book("Dune")?In the constructor Book(String title) { title = title; }, what's the title field after new Book("Dune")?

  • "Dune""Dune"
  • nullnull
  • שגיאת קומפילציהA compile error
  • "title""title"

nullnull

איפה מותר לכתוב this(...) בבנאי?Where may you write this(...) in a constructor?

  • בכל מקוםAnywhere
  • רק בשורה הראשונהOnly on the first line
  • רק בשורה האחרונהOnly on the last line
  • רק מחוץ לבנאיOnly outside a constructor

רק בשורה הראשונהOnly on the first line

תלמיד עם בנאיA student with a constructor

  • מחלקה Student עם בנאי Student(String name, int grade) ומתודה String info() (`Noa:A class Student with a constructor Student(String name, int grade) and a method String info() (`Noa:

צייד באגים: הכותרת שנעלמהBug hunter: the vanishing title

  • התוכנית מדפיסה null במקום Dune, ולא קורסת.The program prints null instead of Dune, and doesn't crash.

פיצה עם ברירת מחדלA pizza with a default

  • מחלקה Pizza עם size ו-topping.A class Pizza with size and topping.

צייד באגים: אין בנאי ריקBug hunter: no empty constructor

  • התוכנית מהתזכורת לא מתקמפלת.The program from the refresher doesn't compile.

משך זמןA duration

  • מחלקה Duration ששומרת int seconds.A class Duration storing int seconds.

מה למדנוWhat we learned

  • בנאי: "מתודה" מיוחדת בשם המחלקה, בלי טיפוס החזרה, שרצה ב-new:constructor: A special "method" named after the class, with no return type, that runs on new:
  • בנאי רץ ב-new ומקבל ערכים.A constructor runs on new and takes values.
  • בנאי קצר מעביר את העבודה לבנאי המלא עם this(title, "unknown").A short constructor hands the work to the full one with this(title, "unknown").
  • ברגע שכתבתם Book(String title), Java כבר לא נותנת Book().As soon as you write Book(String title), Java no longer gives you Book().
  • כשהבנאי מתחיל, a כבר 5 (האתחול שליד השדה רץ קודם), ו-b עדיין 0.When the constructor starts, a is already 5 (the initializer next to the field ran first), and b is still 0.
  • constructor Book in class Book cannot be applied to given types;:constructor Book in class Book cannot be applied to given types;:

עכשיו לתרגולNow, practice

6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
הערות מרצהSpeaker notes

נבנה על ידיBuilt by Ariel Yohimovich