0

I'm new to java ,I can't understand what's the difference between static methods-variables and non-static method-variables ,Can you help me , Thank You Guys.

Yacine_Dev_Artist
  • 135
  • 1
  • 2
  • 9
  • 1
    `static` - one per class (a global). not `static` - one per instance. – Elliott Frisch Mar 08 '20 at 17:05
  • https://stackoverflow.com/questions/2649213/in-laymans-terms-what-does-static-mean-in-java – Jocke Mar 08 '20 at 17:07
  • 1
    Does this answer your question? [In laymans terms, what does 'static' mean in Java?](https://stackoverflow.com/questions/2649213/in-laymans-terms-what-does-static-mean-in-java) – Jocke Mar 08 '20 at 17:07

1 Answers1

0

This is a very good article, feel free to read it. You can think of static as simply meaning a single copy. Take the following example;

class A {

    private static int a;

    public A(int a) {
        this.a = a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public int getA() {
        return a;
    }

}

Class A has a method called getA() that returns the value of a. Note that the variable a is static.

A a1 = new A(0);

A a2 = new A(1);

a1.setA(2);

// both values would be 2 since a1 and a2 share the same single copy.

int a1a = a1.getA();

int a2a = a2.getA();
Jason
  • 5,154
  • 2
  • 12
  • 22