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

חזרה למפגשBack
JavaJava

static מול instancestatic vs instance

מפגש 10: מה שייך למחלקה ומה שייך לעצם, מתי static הגיוני, ולמה main הוא static.Session 10: What belongs to the class and what belongs to an object, when static makes sense, and why main is static.

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

  • מונחיםTerms
  • לכל עצם עותק משלוEvery object has its own copy
  • שדה static: עותק אחד לכולםA static field: one copy for everyone
  • למה main הוא static, ומה קורה כששוכחיםWhy main is static, and what happens when you forget
  • טעויות נפוצותCommon mistakes
  • פרויקט מדףShelf project
נושאTopic 1

מונחיםTerms

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

  • חבר 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 (כמו main).static context: Code inside a static method (like main).
  • קבוע: static final:constant: static final:
  • מחלקת עזר: מחלקה שכל המתודות שלה static, כמו Math ו-Arrays:utility class: A class whose methods are all static, like Math and Arrays:
נושאTopic 2

לכל עצם עותק משלוEvery object has its own copy

לכל עצם עותק משלוEvery object has its own copy

  • שדה בלי 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.
נושאTopic 3

שדה static: עותק אחד לכולםA static field: one copy for everyone

שדה static: עותק אחד לכולםA static field: one copy for everyone

  • 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.
נושאTopic 4

למה main הוא static, ומה קורה כששוכחיםWhy main is static, and what happens when you forget

למה main הוא static, ומה קורה כששוכחיםWhy main is static, and what happens when you forget

  • ה-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.
  • javac עוצר עוד לפני הריצה.javac stops before anything runs.
נושאTopic 5

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

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

  • non-static method square(int) cannot be referenced from a static context:non-static method square(int) cannot be referenced from a static context:
נושאTopic 6

פרויקט מדףShelf project

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

  • מדף, צעד 10: מתודות עזר סטטיותShelf, step 10: static helper methods

חבר 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 (כמו main).static context: Code inside a static method (like main).

קבוע: static final:constant: static final:

מחלקת עזר: מחלקה שכל המתודות שלה static, כמו Math ו-Arrays:utility class: A class whose methods are all static, like Math and Arrays:

שדה בלי 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.

javac עוצר עוד לפני הריצה.javac stops before anything runs.

non-static method square(int) cannot be referenced from a static context:non-static method square(int) cannot be referenced from a static context:

מדף, צעד 10: מתודות עזר סטטיותShelf, step 10: static helper methods

כמה עותקים יש לשדה static int count כשיש 5 עצמים?How many copies of static int count are there with 5 objects?

  • 55
  • 11
  • 00
  • 66

11

איך קוראים למתודה סטטית max3 במחלקה MathUtil מתוך Main?How do you call static method max3 of class MathUtil from Main?

  • MathUtil.max3(1, 2, 3)MathUtil.max3(1, 2, 3)
  • new MathUtil.max3(1, 2, 3)new MathUtil.max3(1, 2, 3)
  • max3(1, 2, 3)max3(1, 2, 3)
  • MathUtil->max3(1, 2, 3)MathUtil->max3(1, 2, 3)

MathUtil.max3(1, 2, 3)MathUtil.max3(1, 2, 3)

למה main חייב להיות static?Why must main be static?

  • כי הוא מהיר יותרBecause it's faster
  • כי ה-JVM קוראת לו לפני שיש עצם כלשהוBecause the JVM calls it before any object exists
  • כי אסור למחלקה Main שדותBecause Main can't have fields
  • זה לא חובהIt isn't required

כי ה-JVM קוראת לו לפני שיש עצם כלשהוBecause the JVM calls it before any object exists

מחלקת עזרA utility class

  • כתבו מחלקה MathUtil (באותו קובץ, מתחת ל-Main) עם static int max3(int a, int b, int c).Write a class MathUtil (in the same file, below Main) with static int max3(int a, int b, int c).

קבוע מע"מA VAT constant

  • הגדירו קבוע static final double VAT = 0.17; ומתודה static double withVat(double price).Define a constant static final double VAT = 0.17; and a method static double withVat(double price).

מונה מבקריםA visitor counter

  • כתבו מחלקה Visitor עם שדה static int total ושדה String name.Write a class Visitor with a field static int total and a field String name.

צייד באגים: from a static contextBug hunter: from a static context

  • התוכנית לא מתקמפלת.The program doesn't compile.

קליקים: של כל אחד ושל כולםClicks: each one's and everyone's

  • במחלקה Counter יש int count (לכל מונה) ו-static int all (לכולם יחד).Class Counter has int count (per counter) and static int all (for all of them together).

מדף, צעד 10: מתודות עזר סטטיותShelf, step 10: static helper methods

  • כתבו מחלקת עזר ShelfUtil עם static String stars(int rating) (למשל 4 → ****-) ו-static double average(int[] ratings).Write a utility class ShelfUtil with static String stars(int rating) (e.g.

מחולל מזהיםAn ID generator

  • כתבו מחלקה IdGen עם static int next = 100 ו-static String nextId(String prefix) שמחזירה מזהה כמו B-100 ומקדמת את next.Write a class IdGen with static int next = 100 and static String nextId(String prefix) returning an id like B-100 and advancing next.

מתודה סטטית שעובדת על עצמיםA static method that works on objects

  • במחלקה Book (String title, static int created) כתבו static Book longest(Book[] books) שמחזירה את הספר עם השם הארוך ביותר.In class Book (String title, static int created) write static Book longest(Book[] books) returning the book with the longest title.

מה למדנוWhat we learned

  • חבר static: שדה או מתודה ששייכים למחלקה עצמה, בעותק אחד:static member: A field or method that belongs to the class itself, in one copy:
  • שדה בלי static הוא שדה של העצם: ל-a ול-b יש n נפרד.A field without static is an object's field: a and b each have their own n.
  • static int count שייך למחלקה: יש רק אחד, וכל עצם חדש מעלה אותו.static int count belongs to the class: there's only one, and each new object bumps it.
  • ה-JVM קוראת ל-main בלי ליצור עצם, ולכן הוא static.The JVM calls main without creating an object, so it's static.
  • non-static method square(int) cannot be referenced from a static context:non-static method square(int) cannot be referenced from a static context:
  • מדף, צעד 10: מתודות עזר סטטיותShelf, step 10: static helper methods

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

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

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