Post a New Question

Computers

posted by .

Hi, does anyone know the difference between "public int getId()" and "public static int getId()" and when would you use them?

thanks

  • Java -

    The keyword static is used to qualify variables and methods within a class. When used, the one single copy (instance) of the associated variable or method will be shared within the class among the zero or multiple instances of the class.

    To give an example:

    public Anything class{
    static int total=0;
    public add(int n){total+=n;)
    public multiply(int n){total*=n;)
    }
    public static int main(String[] args)
    {
    Anything a=new Anything();
    Anything b=new Anything();
    a.add(2);
    b.multiply(7);
    // a.total now has a value of 14.
    }

Answer This Question

First Name:
School Subject:
Answer:

Related Questions

More Related Questions

Post a New Question