חלקPart 2: מתודות ומערכיםMethods & arrays
כ-3 שעות (כולל הפסקה)About 3 hours (with a break)
static מול instancestatic vs instance
מה שייך למחלקה ומה שייך לעצם, מתי static הגיוני, ולמה main הוא static.
What belongs to the class and what belongs to an object, when static makes sense, and why main is static.
הנקודות החשובותKey points
- חבר static: שדה או מתודה ששייכים למחלקה עצמה, בעותק אחד:static member: A field or method that belongs to the class itself, in one copy:
- חבר מופע: שדה או מתודה בלי static:instance member: A field or method without static:
- שדה בלי static הוא שדה של העצם: ל-
aול-bישnנפרד.A field without static is an object's field:aandbeach have their ownn. - בזיכרון רואים שני עצמי
Counterשונים ב-heap.In memory you see two separateCounterobjects on the heap. static int countשייך למחלקה: יש רק אחד, וכל עצם חדש מעלה אותו.static int countbelongs to the class: there's only one, and each new object bumps it.- ב-stack הוא מופיע בתור
static, מחוץ לכל עצם, ו-idהוא של כל כרטיס בנפרד.In the stack it shows up asstatic, outside every object, whileidbelongs to each ticket. - ה-JVM קוראת ל-
mainבלי ליצור עצם, ולכן הוא static.The JVM callsmainwithout creating an object, so it's static. - מתוך הקשר סטטי אי אפשר לקרוא למתודה של עצם.From a static context you can't call an object's method.
non-static method square(int) cannot be referenced from a static context:non-static method square(int) cannot be referenced from a static context:
מונחיםTerms
- חבר 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. - חבר מופעinstance member: שדה או מתודה בלי static: לכל עצם יש עותק משלו של השדה (
a.nו-b.nנפרדים), וקוראים למתודה דרך עצם.A field or method without static: every object has its own copy of the field (a.nandb.nare separate), and you call the method through an object. - הקשר סטטיstatic context: קוד בתוך מתודה static (כמו
main). אין שםthis, ולכן אי אפשר לפנות ישירות לשדה או למתודה של עצם:non-static method greet() cannot be referenced from a static context.Code inside a static method (likemain). There's nothisthere, so you can't reach an object's field or method directly:non-static method greet() cannot be referenced from a static context. - קבוע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;. - מחלקת עזרutility class: מחלקה שכל המתודות שלה static, כמו
Mathו-Arrays: אוסף כלים, בלי מצב משלה.A class whose methods are all static, likeMathandArrays: a toolbox with no state of its own. - חבר 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. - חבר מופעinstance member: שדה או מתודה בלי static: לכל עצם יש עותק משלו של השדה (
a.nו-b.nנפרדים), וקוראים למתודה דרך עצם.A field or method without static: every object has its own copy of the field (a.nandb.nare separate), and you call the method through an object. - הקשר סטטיstatic context: קוד בתוך מתודה static (כמו
main). אין שםthis, ולכן אי אפשר לפנות ישירות לשדה או למתודה של עצם:non-static method greet() cannot be referenced from a static context.Code inside a static method (likemain). There's nothisthere, so you can't reach an object's field or method directly:non-static method greet() cannot be referenced from a static context. - קבוע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;. - מחלקת עזרutility class: מחלקה שכל המתודות שלה static, כמו
Mathו-Arrays: אוסף כלים, בלי מצב משלה.A class whose methods are all static, likeMathandArrays: a toolbox with no state of its own.
Cheatsheet
לכל עצם עותק משלוEvery object has its own copy
Counter a = new Counter();
Counter b = new Counter();
a.click();
a.click();
b.click();
System.out.println(a.n + " " + b.n);שדה static: עותק אחד לכולםA static field: one copy for everyone
Ticket a = new Ticket("Noa");
Ticket b = new Ticket("Avi");
System.out.println(a.id + " " + b.id + " " + Ticket.count);שגיאות שפגשנוErrors we met
| שגיאהError | מה זה אומרWhat it means | מה עושיםWhat to do |
|---|---|---|
non-static method greet() cannot be referenced from a static | למה main הוא static, ומה קורה כששוכחיםWhy main is static, and what happens when you forget | non-static method greet() cannot be referenced from a static context: greet שייכת לעצם, ו-main רץ בלי עצם.non-static method greet() cannot be referenced from a static context: greet belongs to an object, and main runs without one. |
non-static method square(int) cannot be referenced from a st | צייד באגים: from a static contextBug hunter: from a static context | non-static method square(int) cannot be referenced from a static context: square היא מתודת מופע, ו-main סטטי. מוסיפים static ל-square (היא לא צריכה שום שדה של עצם).non-static method square(int) cannot be referenced from a static context: square is an instance method and main is static. Add static to square (it uses no field of any object). |