Q:

Why arrays declared in parent class not visible in child class during inheritance in java?

class Parent
{
    public static void main (String[] args) throws java.lang.Exception
    {
        int[] array=new int[]{1,2,3,4,5};
    }
}
class Child extends Parent
{
    int x=array[2];
    public void MyPrint()
    {
        System.out.println(x);
    }
}

The error which I am getting is,

Main.java:18: error: cannot find symbol int x=array[2];
        ^ symbol:   variable array location: class Child 1 error

Yes we can pass the array as arguments to the method and the code will compile. But why is this approach leading to an error even though the class and method in which the array has been declared is public?

A:

But why is this approach leading to an error even though the class and
method in which the array has been declared is public?

even though the method is declared public does not mean you can get access to objects declared inside of it...
int[] array belongs to the static method main and not to the class, the scope is compl. different and you will never be able to read/write that object unless you modify the scope..

