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.
    }

Respond to this Question

First Name
School Subject
Your Answer

Similar Questions

  1. Java

    1 public class testOperators 2 { 3 public static void main(String[] args) 4 { 5 int x; 6 int y = 12 7 double z = 13.0; 8 x = 14 9 System.out.println(“x + y + z = “ + (x + y +z)); 10 x += y; 11 y--; 12 z++; 13 z *= x; 14 System.out.println(“x …
  2. Java

    I need help putting the following trace into pseudocode 1 public class testOperators 2 { 3 public static void main(String[] args) 4 { 5 int x; 6 int y = 12 7 double z = 13.0; 8 x = 14 9 System.out.println(“x + y + z = “ + (x + …
  3. Computer Science

    //program that puts two lists in order in one single list with all the numbers in order import java.util.Scanner; public class listOrder { public static void main (String [] args){ int [] list1; int [] list2; int [] list3; int temp; …
  4. Computers

    //program that puts two lists in order in one single list with all the numbers in order import java.util.Scanner; public class listOrder { public static void main (String [] args){ int [] list1; int [] list2; int [] list3; int temp; …
  5. shabou

    Can someone please make a Binarytree of these methodes in Java?
  6. computer science

    Im trying to create an arraylist that's sorted but I have no idea how to sort the numbers. I cant use the array.sort method here is my code: public class Testing { private int [] data; private int count; private static int defCapacity …
  7. programming

    m trying to create an arraylist that's sorted but I have no idea how to sort the numbers. I cant use the array.sort method here is my code: public class Testing { private int [] data; private int count; private static int defCapacity …
  8. Computer Science

    How would I put this into one program? These are the directions: Write a method that takes two parameters. If the first parameter is equal to second parameter. Multiply both and print result. If the first parameter is less than the
  9. Computer Programming

    I already posted this question but I gave it a try I'm not sure if I'm on the right track: Make a util class with three methods. First method's name is add, that returns the result by adding two numbers. Second method's name is multiply, …
  10. Java programming

    Hi, I'm trying to figure out how to implement toString() methods into the JavaTest and H2ClassB classes so that the output would be 4 3 7 5 99 3: import java.util.ArrayList; public class JavaTest { ArrayList <JavaTest> list = …

More Similar Questions