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

JavaJava
16
חלקPart 3: OOP: היסודותOOP: the basics כ-3 שעות (כולל הפסקה)About 3 hours (with a break)

static, final וקבועיםstatic, final & constants

שדות static משותפים, final למשתנים, לשדות ולמחלקות, וקבועים בשמות גדולים.

Shared static fields, final for variables, fields and classes, and constants in capital letters.

או Ctrl+Por Ctrl+P

הנקודות החשובותKey points

  • הפניה final: final int[] a אומר שאי אפשר להצביע עם a על מערך אחר.final reference: final int[] a means a can't be pointed at another array.
  • מספר קסם: מספר שמופיע בקוד בלי הסבר (0.17, 56).magic number: A number in the code with no explanation (0.17, 56).
  • משתנה final מקבל ערך פעם אחת.A final variable gets a value once.
  • ניסיון לשנות אותו נעצר בקומפילציה.Trying to change it is stopped at compile time.
  • MIN_RATING ו-MAX_RATING הם קבועים של המחלקה.MIN_RATING and MAX_RATING are class constants.
  • count הוא מונה static אחד, ו-id הוא שדה final של כל ספר, שנקבע פעם אחת בבנאי.count is one static counter, and id is a final field of each book, set once in the constructor.
  • final int[] scores נועל את החץ, לא את המערך: scores[0] = 95 מותר, אבל scores = new int[3] היה נעצר בקומפילציה.final int[] scores locks the arrow, not the array: scores[0] = 95 is allowed, but scores = new int[3] would be stopped at compile time.
  • cannot assign a value to final variable count:cannot assign a value to final variable count:
  • מדף, צעד 16: מונה ספרים וקבועי דירוגShelf, step 16: a book counter and rating constants

מונחיםTerms

  • finalfinal: משתנה final מקבל ערך פעם אחת ולא משתנה יותר. קבועים כותבים באותיות גדולות: final int MAX = 10;.A final variable gets a value once and never changes. Constants are written in capitals: final int MAX = 10;.
  • הפניה finalfinal reference: final int[] a אומר שאי אפשר להצביע עם a על מערך אחר. את התוכן של המערך עדיין אפשר לשנות.final int[] a means a can't be pointed at another array. The array's contents can still change.
  • קבועconstant: static final: ערך אחד למחלקה שאי אפשר לשנות. נהוג לכתוב באותיות גדולות: static final double VAT = 0.17;.static final: one value per class that can't change. Usually written in capitals: static final double VAT = 0.17;.
  • מספר קסםmagic number: מספר שמופיע בקוד בלי הסבר (0.17, 56). קבוע עם שם (VAT, PASS) מסביר את עצמו ומשתנה במקום אחד.A number in the code with no explanation (0.17, 56). A named constant (VAT, PASS) explains itself and changes in one place.
  • חבר staticstatic member: שדה או מתודה ששייכים למחלקה עצמה, בעותק אחד: Ticket.count, Math.max. לא צריך עצם כדי להשתמש בהם.A field or method that belongs to the class itself, in one copy: Ticket.count, Math.max. No object is needed to use them.
  • מתודת יצרןfactory method: מתודה static שמחזירה עצם חדש, עם שם שמסביר מה מקבלים: Temperature.ofCelsius(100). לעתים קרובות הבנאי עצמו private.A static method that returns a new object, with a name explaining what you get: Temperature.ofCelsius(100). Often the constructor itself is private.
  • מחלקת עזרutility class: מחלקה שכל המתודות שלה static, כמו Math ו-Arrays: אוסף כלים, בלי מצב משלה.A class whose methods are all static, like Math and Arrays: a toolbox with no state of its own.
  • הפניה finalfinal reference: final int[] a אומר שאי אפשר להצביע עם a על מערך אחר. את התוכן של המערך עדיין אפשר לשנות.final int[] a means a can't be pointed at another array. The array's contents can still change.
  • מספר קסםmagic number: מספר שמופיע בקוד בלי הסבר (0.17, 56). קבוע עם שם (VAT, PASS) מסביר את עצמו ומשתנה במקום אחד.A number in the code with no explanation (0.17, 56). A named constant (VAT, PASS) explains itself and changes in one place.
  • מתודת יצרןfactory method: מתודה static שמחזירה עצם חדש, עם שם שמסביר מה מקבלים: Temperature.ofCelsius(100). לעתים קרובות הבנאי עצמו private.A static method that returns a new object, with a name explaining what you get: Temperature.ofCelsius(100). Often the constructor itself is private.

Cheatsheet

קבועים ומונה מזהיםConstants and an id counter
Book a = new Book("Dune", 5);
Book b = new Book("Emma", 4);
System.out.println(a.getId() + " " + b.getId() + " of " + Book.getCount());
final על הפניהfinal on a reference
final int[] scores = {70, 80};
scores[0] = 95;
System.out.println(Arrays.toString(scores));

שגיאות שפגשנוErrors we met

שגיאהErrorמה זה אומרWhat it meansמה עושיםWhat to do
cannot assign a value to final variable maxfinal: השמה אחת בלבדfinal: one assignment onlycannot assign a value to final variable max: max כבר קיבל 3.cannot assign a value to final variable max: max already got 3.
cannot assign a value to final variable countצייד באגים: מונה שאסור לו לזוזBug hunter: a counter that can't movecannot assign a value to final variable count: מונה חייב להשתנות, ולכן הוא לא יכול להיות final.cannot assign a value to final variable count: a counter must change, so it can't be final.

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