-5

This is a part of code i was working on .. but the compiler showed an error on line 1. (Syntax error on token";", , expected). Why is that error coming??

public class variable 
{

           int[] nums;
           nums= new int[7];
}
Jens
  • 67,715
  • 15
  • 98
  • 113
H.patel
  • 1
  • 3

2 Answers2

2

You have to initialize the Array in same line as the declaration

public class variable 
{

           int[] nums = new int[7];
}

or you have to initialize it in a method or constructor:

 public class variable 
{

   int[] nums;
   public variable(){

           nums= new int[7];
   }
}

Hint: read about Java naming conventions. Class names should start with uppercase character.

Jens
  • 67,715
  • 15
  • 98
  • 113
1

You should use assignment inside a method or a constructor. Or you can instantiate it class level but you have to initialize it same line with declaration.

Eg: Class level instantiation.

public class Variable {
    int[] nums = new int[7];
}

Use inside a method.

public class Variable {
    int[] nums;
    public void method(){            
        nums = new int[7];
    }
}
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115