Difference between 'static int' and 'int' in java

in java, where and when do we use "static int" and how does it differ from "int"

+3
source share
7 answers

Look here: Understanding Instance and Class Members

When a series of objects is created from one plan, each of them has its own copies instance variables. (...)

Sometimes you want to have variables common to all objects. This is achieved using a static modifier. Fields that have a static modifier in the declaration are called static fields or class variables.

+9
source

Well.

-, static , .

, static "" . , , ( ).

static, ( ). , , .

( ).

+3

static , . class. .

public static final int MAX = 10000;  // Defined in MyClass

// Somewhere else you could do
int max = MyClass.MAX;  // notice no instance of MyClass needed.

: , , .

+2

static int: .

INT: .

+1

static , , . , .

:

class Test {
  static int i;
  int j;
}

class Test 2 {
   public static void main(String args[])  {
     Test test1 = new Test();
     Test test2 = new Test();
     test1.i = 1;
     test1.j = 2;
     test2.i = 3;
     test2.j = 4;
     System.out.println("test1.i: "+test1.i);
     System.out.println("test1.j: "+test1.j);
     System.out.println("test2.i: "+test2.i);
     System.out.println("test2.j: "+test2.j);
   }
}

2 Test, "" i. j.

test1.i: 3
test1.j: 2
test2.i: 3
test2.j: 4

Java -

+1

'int' , . "static int" , ( )

0

static int, , . int, .

0

Source: https://habr.com/ru/post/1731353/


All Articles