0

how int arr[] gets null and how main method a[] gets error

      public class Main
      {
       int arr[];
       public static void main(String[] args) 
       {
        int a[];
        Main m=new Main();
    
       System.out.println("Main class array"+m.arr); //null
       System.out.println("Main method array"+a);   //error
       }
     }
3
  • 1
    You never initialize m.arr so, being an instance member, it gets a default value of null, and you never initialize a so, being a local variable, it's a compile-time error. Commented Aug 30, 2022 at 6:39
  • 1
    Member variables, which are variables defined in a class, such as arr in your example, are initialized to null by default. But local variables, which are variables defined inside a method, such as a, are not initialized at all by default. You must initialize them explicitly, otherwise the compiler will complain that you're trying to use an uninitialized variable. Commented Aug 30, 2022 at 7:11
  • Good reads: What are java object fields initialized with? and Variable default value in Java Commented Aug 30, 2022 at 8:09

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.