Good day. I understand Java and the question arose:
If I make the connections array static and use it in the getElementOfArray() method, then with System.out.println(getElementOfArray()); it displays the (last) element of the array (which is what is needed).
But if I want the array to belong to an object and not to a class, and create a ConnectionManager object to access the array, then System.out.println(getElementOfArray()); displays only null .

Question:

  1. What object is created, at ConnectionManager cm = new ConnectionManager();
  2. Will this object start with psvm
  3. Why is it that everything goes well with a static array, but not when creating an object?

thank you very much

 public class ConnectionManager {
     private Connection1 [] connections = new Connection1 [5];  // Array for Connection1
     private static int counter;

     private void fillArray () {
         for (int i = 0; i <connections.length; i ++) {// Fills the array
             connections [i] = Connection1.getInstance ();  // elements of connection1
             counter ++;
             System.out.println (connections.getClass () + "Element added");
         }
     }

     static Connection1 getElementOfArray () {
         ConnectionManager cm = new ConnectionManager ();  // Create an object
         if (counter> 0) {
             return cm.connections [- counter];
         } else {
             return null;
         }
     }

     public static void main (String [] args) {
         ConnectionManager cm = new ConnectionManager ();
         cm.fillArray ();
         System.out.println (getElementOfArray ());
     }
 }
  • and when debugging what he writes? since null comes in, it means there are 0 elements. - Senior Pomidor
  • @SeniorAutomator I understand this, but I want to understand why this is happening - hidd
  • one
    you have private Connection1 [] connections is not static. make it static and then you will understand what to take. otherwise you refer to an array whose elements are not filled and not initialized - Senior Pomidor

2 answers 2

You have two ConnectionManager objects. In the main method, one object, and in the getElementOfArray method, another, respectively, if the array is not static, it accesses different arrays.

Those. The fillArray method fills an array of another object that is not accessed.

I hope, clearly explained =)

  • But when creating an object in the getElementOfArray() method, as I understand it, its psvm should be called and the array should be filled accordingly. Right? - hidd
  • psvm is what? - Nikolay
  • one
    No, not right. psvm is called here only once, when you start the program. fillArray fills a completely different array. - Artem Konovalov
  • @Nikolay public static void main (String [] args) {} - hidd
  • one
    @ArtemKonovalov Thank you) - hidd

you do not fill the array!

 public static void main(String[] args) { ConnectionManager cm = new ConnectionManager(); cm.fillArray(); System.out.println(getElementOfArray()); } 

here you create an object and fill an array in it.

And then you create a new object and do not fill in anything in it:

 static Connection1 getElementOfArray(){ ConnectionManager cm = new ConnectionManager(); //Cоздание объекта if(counter > 0){ return cm.connections[--counter]; } else { return null; } }