IDentifier expected!

+2 Arnab Banerjee · January 22, 2015
class prog1{
int a = 5, b = 6,c = 0;
c = a + b;
public static void main(String args[]){
System.out.println("The result is " + c );
}
}

What is wrong in this code ? :P  can't identify it. it is saying, "identifier expected".

Post a Reply

Replies

Oldest  Newest  Rating
0 Tim Herreijgers · January 22, 2015
The static method main can't reference the non-static integers a, b and c. This means you will have to change the line where you initialize your variables to this:
static int a = 5, b = 6, c = 0;

Next you are add two numbers together outside a method. You have to move the line 
c = a + b;


inside the main method. At it the end it should look like this:


public class prog1 {
static int a = 5, b = 6, c = 0;
public static void main(String[] args){
c = a + b;
System.out.println("The result is " + c);
}
}

This code will show this : "The result is 11" in the console.
0 Neil Cannon · January 24, 2015
class Calculate{

    public static void main (String [] args){
        
        int a = 5;

        int b = 6;

        int c = a + b;
        
        c = a + b;
    
        System.out.println("The result is " + c);
        
    }
}
  • 1

Java / Android Development

106,922 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator