Syntax error in java? i have tried this program its showing error like this in Eclipse "Syntax error on token "i", VariableDeclarator expected after this token" . in last line of the program "objectname.i"
What is the reason for this error??How to access the variable "i"
import java.util.Scanner; public class constructors { int i ; private int j; private int k;
public static void main(String args[]) { System.out.println("Enter the name:");
} } class constructor{ constructors objectname = new constructors(); Scanner myScanner = new Scanner(System.in); objectname.i ; }
Hello Friend,
Actually your syntax is not correct.
Anyways, try the following code:
import java.util.*; class constructor{ int i=0; constructor(){ Scanner input=new Scanner(System.in); System.out.print("Enter number: "); i=input.nextInt(); } } public class constructors{ public static void main(String args[]){ constructor con=new constructor(); System.out.println(con.i); } }
Thanks
Ads