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

10th Jan 2019, 5:51 AM
ashutosh
ashutosh - avatar
4 Respostas
+ 8
You need to make your variable public to access like this.
10th Jan 2019, 6:04 AM
blACk sh4d0w
blACk sh4d0w - avatar
10th Jan 2019, 5:58 AM
Gordon
Gordon - avatar
+ 2
You have to declare an assign value at the same line: class Simple { int i=10; }
10th Jan 2019, 6:03 PM
Javier Felipe Toribio
Javier Felipe Toribio - avatar
0
You can't assign a value outside a method or constructor.. You can use int i = 10; instead
10th Jan 2019, 3:08 PM
Robert Sokolov
Robert Sokolov - avatar