استدعاء طرق الفئة
مُقدَّم| English | المعنى |
|---|---|
| class method/klæs ˈmeθəd/ | دالة صنفية |
| static method/ˈstætɪk ˈmeθəd/ | دالة ثابتة |
Calling a class method
- A class method 类方法 (a static method 静态方法) belongs to the class itself, not to an object.
- Call it with the class name and a dot:
Math.sqrt(16),Integer.parseInt("42"). - No object needs to exist — the method lives on the class.
- The keyword
staticin the header marks a method as a class method.
Passing arguments
- Put the arguments inside the parentheses:
Math.max(3, 8). - The values are matched to the method's parameters, by position.
- Give the right number and types of arguments, or it won't compile.
- No arguments? Still write the empty parentheses:
sc.nextLine().

A class method is called on the class name: Math.sqrt(25.0)
Using the return value
- Most class methods return a value — capture it in a variable.
double r = Math.sqrt(16);stores4.0inr.- Ignoring a useful return value usually means you forgot to store the result.
- A returned value can also feed straight into another expression.
Static vs. instance — a first look
- A class (static) method is called on the class:
Math.abs(-5). - An instance method (next lessons) is called on an object:
word.length(). - The tell: is there a dot after a class name or after an object?
- Class methods don't need an object; instance methods do.
Call a class method on the CLASS name, not an object: Math.sqrt(16), never m.sqrt(16). Match the argument count and types to the method's parameters, and remember to store the return value — Math.sqrt(16); on its own computes 4.0 and then throws it away. Empty parentheses are still required when there are no arguments.
Combining class method calls:
int bigger = Math.max(3, 8);→biggeris8.double root = Math.sqrt(bigger + 1);→Math.sqrt(9)→3.0.- A return value (
8) fed straight into the next call.
A class (static) method is called on the class name: Math.sqrt(16). Pass the right arguments (matched by position and type) in parentheses, and store the return value in a variable to use it. This differs from an instance method, which is called on an object.
استدعاء طريقة static
يُستدعى Math.max على الفئة ويعيد القيمة الأكبر.
كيف تستدعي طريقة sqrt الثابتة من فئة Math بشكل صحيح؟
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإكمال الأول اليوم
تُستدعى طرق الفئة باسم الفئة مع نقطة.
ماذا ترجم Math.max(3, 8)؟
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإكمال الأول اليوم
max تعيد الأكبر بين الحججين.
الكلمة المفتاحية التي تُحدد الطريقة كطريقة فئة (static) هي...
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإكمال الأول اليوم
static يعني أن الطريقة تنتمي للفئة، وليس لكائن.
Math.sqrt(16); في سطر مستقل يخزن 4.0 للاستخدام لاحقاً.
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإكمال الأول اليوم
يحتسب 4.0 لكن يتخلى عنها — يجب عليك تخزين قيمة الإرجاع.
تُستدعى طريقة الفئة على اسم الفئة، بينما تُستدعى طريقة العنصر على كائن.
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإكمال الأول اليوم
Math.abs(-5) (class) مقابل word.length() (object).