0
why we can't assign a value to an instance variable after its declaration?
Input :-- class Simple { int i; i=10; } class Dcoder { public static void main(String args[]) { Simple s=new Simple(); System.out.println("Hello, Dcoder!"); System.out.println(s.i); } } Output :-- source_file.java:4: error: <identifier> expected i=10; (why is this happening?) ^ 1 error
4 ответов
+ 8
You need to make your variable public to access like this.
+ 2
public int i
https://www.sololearn.com/learn/Java/2156/
+ 2
You have to declare an assign value at the same line:
class Simple
{
int i=10;
}
0
You can't assign a value outside a method or constructor.. You can use int i = 10; instead