How to declare a dynamic object array in Java?

后端 未结 4 1129
一整个雨季
一整个雨季 2020-12-31 02:23

I want to ask a question about Java. I have a user-defined object class, student, which have 2 data members, name and id. And in another class, I have to declare that object

4条回答
  •  不知归路
    2020-12-31 03:20

    Its not possible,we need to specify the size of array when declaring object array;

    1. one way to declare object array

       student st[];
       st=new student[3];
      
    2. second way

       student st[]=new student[5];
      

    in both cases not any objects are created only the space is allocated for the array.

    st=new student[1];
    

    this will create a new object;

提交回复
热议问题