0
   `

class under
    {

           static int[] locations ={2,3,4};//arrays declared with values in class under

    }
    class understand
    {
        String c;
        public static void main(String[] args)
        {

            under d=new under();
            System.out.println(under.locations[0]);

             understand[] u;//next array created created in the main class using //reference//
            u = new understand[2];
            u[0]=new understand();
            u[0].c="ab";
            System.out.println(u[0].c);


        }
    }` 
        I have the following questions in this code they are:

1.)The first array that I create is of integer type named locations with values inside it.Why do I have make it static in order access it in the main class?

2.)The second concern is the array created in the main class using reference will not be created if i don't name it same as a class. Every time I create using array with reference ,I have to have the same class of it. Why do I need to have a class of the same as the array ? What is the default type of the array created in the main? . The array with reference for example: understand[] array in the main ,if created out the main with references will not be accepted. Can I create a referenced array with out a class of it?

3.)Rather than creating the array using references ,I can directly give values to it then use it accordingly.Why do I need a reference to an array?

Thanks a lot for having attention. Your opinion on this is very valuable to me.It means a lot when my doubts get cleared by the erudite members of Stack overflow.

        enter code here
  • possible duplicate of [Why is there a problem with a non-static variable being read from main?](http://stackoverflow.com/questions/4665089/why-is-there-a-problem-with-a-non-static-variable-being-read-from-main) – Raedwald Sep 13 '14 at 07:26

3 Answers3

0
  1. main() method is static, you can only run static code in static context

  2. I'm sorry bu most of your questions in point 2 don't make sense...

  3. You need reference to every object in Java because objects without reference are collected by garbage collector (permanently removed from memory)

EDIT: I tried reading point 2 again - still don't get it...

Lucas
  • 3,181
  • 4
  • 26
  • 45
0

You cannot make a static reference to a non-static field.

Like ordinary objects, arrays are accessed through references.

Farhan stands with Palestine
  • 13,890
  • 13
  • 58
  • 105
0
  1. Just like Lucas wrote: you can only run static code in static context
  2. There is no such thing as default type of an array created in main. Referring to other questions: array definition in java looks like that:

    T id = new T[initialsize]; T id = new T() { instanceofT_1, instanceofT_2};

So in your case you didn't name an array with the same name as class. What you did was a definition of array u of type understand[].

  1. Again, as Lucas wrote: every object in Java can not exist without reference. Java compiler will not allow you to write something like below:

    new String[] {};

It'll report an error "The left-hand side of an assignment must be a variable".

Arek
  • 3,106
  • 3
  • 23
  • 32