Java

Static vs instance methods

Në shqip: Metodat statike dhe të objektit

ShpjegimiSQ

Një metodë static i përket klasës dhe thirret pa objekt (Math.max()); një metodë e zakonshme i përket objektit dhe ka nevojë për të (lojtari.shenon()). Gabimi „non-static method cannot be referenced from a static context“ vjen nga main-i që thërret metodë jo-static.

EnglishEN

A static method belongs to the class and is called without an object (Math.max()); a normal method belongs to an object and needs one (player.score()). The error “non-static method cannot be referenced from a static context” comes from main calling a non-static method.

Si ta mendosh

Statike është si kalkulatori në mur të klasës; e objektit është si fletorja e secilit nxënës.

Lexoje në anglisht

Static is like the classroom calculator; instance is like each student's notebook.

Shembull kodijava

class Ndihmes {
    static int dyfisho(int x) { return x * 2; }   // Ndihmes.dyfisho(3)
    int gola = 0;
    void shenon() { gola++; }                      // objekti.shenon()
}

Terma të lidhur