נבנה על ידיBuilt by Ariel Yohimovich
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.
מה שייך למחלקה ומה שייך לעצם, מתי static הגיוני, ולמה main הוא static. בסוף המפגש מוסיפים צעד לפרויקט המלווה.What belongs to the class and what belongs to an object, when static makes sense, and why main is static. At the end we add a step to the companion project.מה נעשה היום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
עוברים על סדר היום ומזכירים את הפרויקט המלווה.Walk through the plan and mention the companion project.המונחים של המפגש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:
לפני הקוד, המילים. כל מונח כאן מקושר גם במילון של הקורס.Before the code, the words. Every term here is also linked in the course glossary.נושא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.
שדה בלי static הוא שדה של העצם: ל-a ול-b יש n נפרד. בזיכרון רואים שני עצמי Counter שונים ב-heap.A field without static is an object's field: a and b each have their own n. 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.
static int count שייך למחלקה: יש רק אחד, וכל עצם חדש מעלה אותו. ב-stack הוא מופיע בתור static, מחוץ לכל עצם, ו-id הוא של כל כרטיס בנפרד.static int count belongs to the class: there's only one, and each new object bumps it. 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.
ה-JVM קוראת ל-main בלי ליצור עצם, ולכן הוא static. מתוך הקשר סטטי אי אפשר לקרוא למתודה של עצם. javac עוצר עוד לפני הריצה. התיקון: להוסיף static למתודה, או לקרוא דרך עצם: new Main().greet().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 stops before anything runs. The fix: add static to the method, or call it through an object: new Main().greet().נושא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:
אלה הטעויות שהתרגילים מהסוג "צייד באגים" בנויים סביבן. כל אחת רצה באמת, והפלט הוא מה ש-Java עונה.These are the mistakes the "bug hunter" exercises are built around. Each one really runs, and the output is what Java says.נושאTopic 6
פרויקט מדףShelf project
הצעד של היום בפרויקט מדףToday's step in the Shelf project
- מדף, צעד 10: מתודות עזר סטטיותShelf, step 10: static helper methods
כתבו מחלקת עזר ShelfUtil עם static String stars(int rating) (למשל 4 → ****-) ו-static double average(int[] ratings). עבור Dune 5, Emma 4, Ulysses 2 הדפיסו שורה לכל ספר (Dune *****) ובסוף Average: 3.67.Write a utility class ShelfUtil with static String stars(int rating) (e.g. 4 → ****-) and static double average(int[] ratings). For Dune 5, Emma 4, Ulysses 2 print a line per book (Dune *****) and then Average: 3.67.חבר static: שדה או מתודה ששייכים למחלקה עצמה, בעותק אחד:static member: A field or method that belongs to the class itself, in one copy:
המונחים של המפגשThis session's termsחבר מופע: שדה או מתודה בלי static:instance member: A field or method without static:
המונחים של המפגשThis session's termsהקשר סטטי: קוד בתוך מתודה static (כמו main).static context: Code inside a static method (like main).
המונחים של המפגשThis session's termsקבוע: static final:constant: static final:
המונחים של המפגשThis session's termsמחלקת עזר: מחלקה שכל המתודות שלה static, כמו Math ו-Arrays:utility class: A class whose methods are all static, like Math and Arrays:
המונחים של המפגשThis session's termsשדה בלי static הוא שדה של העצם: ל-a ול-b יש n נפרד.A field without static is an object's field: a and b each have their own n.
לכל עצם עותק משלוEvery object has its own copyבזיכרון רואים שני עצמי Counter שונים ב-heap.In memory you see two separate Counter objects on the heap.
לכל עצם עותק משלוEvery object has its own copystatic int count שייך למחלקה: יש רק אחד, וכל עצם חדש מעלה אותו.static int count belongs to the class: there's only one, and each new object bumps it.
שדה static: עותק אחד לכולםA static field: one copy for everyoneב-stack הוא מופיע בתור static, מחוץ לכל עצם, ו-id הוא של כל כרטיס בנפרד.In the stack it shows up as static, outside every object, while id belongs to each ticket.
שדה static: עותק אחד לכולםA static field: one copy for everyoneה-JVM קוראת ל-main בלי ליצור עצם, ולכן הוא static.The JVM calls main without creating an object, so it's static.
למה main הוא static, ומה קורה כששוכחיםWhy main is static, and what happens when you forgetמתוך הקשר סטטי אי אפשר לקרוא למתודה של עצם.From a static context you can't call an object's method.
למה main הוא static, ומה קורה כששוכחיםWhy main is static, and what happens when you forgetjavac עוצר עוד לפני הריצה.javac stops before anything runs.
למה main הוא static, ומה קורה כששוכחיםWhy main is static, and what happens when you forgetnon-static method square(int) cannot be referenced from a static context:non-static method square(int) cannot be referenced from a static context:
טעויות נפוצות, ומה Java עונהCommon mistakes, and what Java saysמדף, צעד 10: מתודות עזר סטטיותShelf, step 10: static helper methods
הצעד של היום בפרויקט מדףToday's step in the Shelf projectכמה עותקים יש לשדה static int count כשיש 5 עצמים?How many copies of static int count are there with 5 objects?
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.11
נכון: שדה static שייך למחלקה.Right: a static field belongs to the class.איך קוראים למתודה סטטית 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)
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.MathUtil.max3(1, 2, 3)MathUtil.max3(1, 2, 3)
נכון: שם המחלקה, נקודה, שם המתודה.Right: class name, dot, method name.למה 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
שואלים את הכיתה ומצביעים. התשובה בשקופית הבאה.Ask the class and take a vote. The answer is on the next slide.כי ה-JVM קוראת לו לפני שיש עצם כלשהוBecause the JVM calls it before any object exists
נכון.Right.מחלקת עזר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).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.קבוע מע"מ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).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מונה מבקריםA visitor counter
- כתבו מחלקה
Visitor עם שדה static int total ושדה String name.Write a class Visitor with a field static int total and a field String name.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.צייד באגים: from a static contextBug hunter: from a static context
- התוכנית לא מתקמפלת.The program doesn't compile.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.קליקים: של כל אחד ושל כולם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).
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מדף, צעד 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 exercise from the practice page: ask how you would start.מחולל מזהים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.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מתודה סטטית שעובדת על עצמים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.
תרגיל מהעמוד הבא: שואלים איך הייתם מתחילים.An exercise from the practice page: ask how you would start.מה למדנו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
כל אחד אומר דבר אחד שלמד היום.Everyone says one thing they learned today.עכשיו לתרגולNow, practice
6 תרגילים ו-2 אתגרים, עם בדיקה אוטומטית6 exercises and 2 challenges, auto-checked
מפנים לעמוד התרגול.Point to the practice page.