Confusion of reference variables and objects in arrays (array of array objects)

Reference variables and objects are very confusing while creating array objects as an array. Observe the following code.

Demo d[] = new Demo[2];

Many people think, in the above statement, two array objects of Demo class are created. It is wrong.

The statement creates two reference variables of Demo class. That is, d[0] and d[1] are reference variables and not objects. They must be converted into objects before used. That is what is done in the following statements.

d[0] = new Demo();
d[1] = new Demo();

Now, d[0] and d[1] are full-fledged objects and can be used in the program as you like.

For in and outs of Java arrays refer All Array Operations at a Glance

2 Responses

  1. vijaylaksshmi September 21, 2013 / 1:25 pm

    excellent sir, i hava done msc(cs) through distance education(annamalai university). and 33 years old. i dont have any experience in software field. worked as computer teacher in blor.

    now i completed diploma in java(java and advance java). if i want to try in software industry, how to improve more technical knowledge to get job. please guide me.

    • S. Nageswara Rao, Corporate Trainer September 22, 2013 / 6:02 pm

      You must be through with aptitude, reasoning, communication skills etc. Also know C and C++ and Java (upto struts). Then apply to jobs.

Leave a Reply

Your email address will not be published. Required fields are marked *