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];
}
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];
}
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.
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];
}
}