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

JavaJava
10
חלק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.

או Ctrl+Por Ctrl+P

הנקודות החשובות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: a and b each have their own n.
  • בזיכרון רואים שני עצמי Counter שונים ב-heap.In memory you see two separate Counter objects on the heap.
  • static int count שייך למחלקה: יש רק אחד, וכל עצם חדש מעלה אותו.static int count belongs to the class: there's only one, and each new object bumps it.
  • ב-stack הוא מופיע בתור static, מחוץ לכל עצם, ו-id הוא של כל כרטיס בנפרד.In the stack it shows up as static, outside every object, while id belongs to each ticket.
  • ה-JVM קוראת ל-main בלי ליצור עצם, ולכן הוא static.The JVM calls main without 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.n and b.n are 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 (like main). There's no this there, 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, like Math and Arrays: 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.n and b.n are 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 (like main). There's no this there, 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, like Math and Arrays: 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 forgetnon-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 contextnon-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).

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