Array default values for Instance Variables Vs Local Variables

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
abashdesh
Posts: 14
Joined: Sat Jul 17, 2021 8:22 am
Contact:

Array default values for Instance Variables Vs Local Variables

Post by abashdesh »

Are the array default values for local variables same as those for instance variables?
Asking this as generally Instance Variables get default values whereas Local Variables need to be explicitly initialised.
Hence was curious if the same is true for Array values or Array values would be an exception to this?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Array default values for Instance Variables Vs Local Variables

Post by admin »

If by "array values", you mean the contents of an array object then there is no difference between an instance and local array object. It is like any other Java object. It doesn't matter where you create an object, the fields of an object are always initialized (as per default values/instance blocks/constructor). So, all elements of an array object will always be initialized to their default values.

The array variable is also just like any other variable. The rules of initialization of local and instance variables apply to array variables as well. So, a local array variable will not be initialized automatically.

You should write a sample test program and check it out.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 31 guests