having Obj class which in his constructor has System.out.println("Hello world!") ;
I create an array of this class using – Obj[] objArray = new Obj[10] ; and nothing printed , means – no instance of Obj has been called . Is there any way to create such array ,but with instances , beyond create them in for loop ?
Well, since you want to know a way apart from using a for loop, you can do this: –
What happens here is, you are initializing your array reference directly with array elements.
Now the type of actual array object is inferred from type of array reference on the LHS.
So, with that declaration, an array of size 3 (in the above case) is created, with each index in the array initialized with the
instanceofObjclass in the given order.A better way that I would suggest is to use an
ArrayList, in which case, you havedouble-braces initializationto initialize yourListwithout for loop. And plus with an added advantage that you can anytime add new elements to it. As itdynamically increasingarray.