Discussion:
Compile error: java: <identifier> expected
(too old to reply)
Marc B
2013-10-14 13:33:25 UTC
Permalink
Hello,

I am new to Java. I create a reference variable, and then call a final method on that variable but on compilation I get this error:

java: <identifier> expected

pointing to the last line - p.sing();

This is my code:

class MyClass{
public static void main(String[] args){

}
}

class Person {
final void sing() {
System.out.println("la..la..la..");
}
}
class Professor {
final void singalong() {
System.out.println("la..la..la..");
}
Person p = new Person();
p.sing();
}

What does the error mean? What identifier needs to be present on that line p.sing();???

Thanks,

Marc
Eric Sosman
2013-10-14 14:11:32 UTC
Permalink
Post by Marc B
Hello,
java: <identifier> expected
pointing to the last line - p.sing();
class MyClass{
public static void main(String[] args){
}
}
class Person {
final void sing() {
System.out.println("la..la..la..");
}
}
class Professor {
final void singalong() {
System.out.println("la..la..la..");
}
Person p = new Person();
p.sing();
}
What does the error mean? What identifier needs to be present on that line p.sing();???
The message is confusing, because it complains about a by-
product of the actual mistake and not about the mistake itself.
(This often happens with error messages because "the" error
depends on what "the" purpose is understood to be, and you and
the computer don't always share the same notion of "the" purpose.)

In any event, "the" "real" problem is that the `p.sing();'
line is not inside a method (or a constructor, or an initializer
block). You can't just sprinkle free-floating executable statements
willy-nilly around your code; they've got to be contained within
methods (etc.). Your Professor class has one method, named
singalong, and that method contains one executable statement:
the println() call. The remaining lines are outside the method,
and that's why the compiler is unhappy.

Why did it complain about `p.sing();' and not about the line
before it? Because that line is a declaration of the variable `p',
plus an initialization of it. Declarations (and initializers)
*are* permitted outside methods; that's how you declare the member
variables of a class. In your case, every Professor object owns
a `p' variable, initialized to a fresh Person instance when the
Professor itself is created. That's probably not what you meant --
but it *does* have meaning, so the compiler didn't squawk (see the
earlier remarks about "the" purpose).
--
Eric Sosman
***@comcast-dot-net.invalid
Marc B
2013-10-14 15:23:39 UTC
Permalink
Thanks for the explanation! I didn't knowe this and I'm in preparation of the OCA Java 1ZO3 exam so I definitely can use this basic info.

Marc
Roedy Green
2013-10-22 03:33:27 UTC
Permalink
On Mon, 14 Oct 2013 06:33:25 -0700 (PDT), Marc B
Post by Marc B
java: <identifier> expected
see http://mindprod.com/jgloss/compileerrormessages.html

For hints on the causes of various error messages.
--
Roedy Green Canadian Mind Products http://mindprod.com
Unlike many machines, computers require no water once they are
manufactured.
a***@gmail.com
2014-11-07 15:07:49 UTC
Permalink
Post by Marc B
Hello,
java: <identifier> expected
pointing to the last line - p.sing();
class MyClass{
public static void main(String[] args){
}
}
class Person {
final void sing() {
System.out.println("la..la..la..");
}
}
class Professor {
final void singalong() {
System.out.println("la..la..la..");
}
Person p = new Person();
p.sing();
}
What does the error mean? What identifier needs to be present on that line p.sing();???
Thanks,
Marc
I'm new here and have got some issues to post,but can't just get it posted.How do i do that?
r***@gmail.com
2015-08-28 17:17:47 UTC
Permalink
class MyClass
{
public static void main(String[] args)
{
Person p = new Person();
p.sing();
}
}

class Person
{
void sing()
{
System.out.println("la..la..la..");
}
}
class Professor
{
void singalong()
{
System.out.println("la..la..laaaaa..la..");
}
}


NOW ITS OKAY
k***@gmail.com
2017-03-19 10:00:03 UTC
Permalink
Create and call object inside main method
f***@gmail.com
2018-06-22 07:54:17 UTC
Permalink
hello i immediately need help!!!
i am trying to learn overloading and i am in 10 standard so please help me
i am getting identifier expected error

import jav.io.*;
public class Overload
{
double r,side,l,b,AreaSquare,AreaCircle,AreaRectangle;
public void area(side)//this the line where error occurs
{
side1=side;
AreaSquare=side1*side1;
System.out.println("AREA OF SQUARE="+Areasquare);
}
public void area(l,b)
{
l1=l;
b1=b;
AreaRectangle=l1*b1;
System.out.println("AREA OF RECTENGLE="+AreaRectangle);
}
public void area(r)
{
r1=r;
PI=3.14;
AreaCircle=PI*r1*r1;
System.out.println("AREA OF CIRCLE="+AreaCircle);
}
public static void main()throws IOException
{
BufferedReader in=new BufferedReader (new InputStreamReader(System.in));
Overload obj=new Overload();
System.out.println("ENTER THE SIDE OF SQUARE=");
side1=Double.parseDouble(in.readLine());
System.out.println("ENTER THE LENGTH OF RECTANGLE=");
l1=Double.parseDouble(in.readLine());
System.out.println("ENTER THE BREADTH OF RECTANGLE=");
b1=Double.parseDouble(in.readLine());
System.out.println("ENTER THE RADIUS OF CIRCLE=");
r1=Double.parseDouble(in.readLine());
obj.area(side);
obj.area(l,b);
obj.area(r);
}
}
Jukka Lahtinen
2018-06-23 11:37:35 UTC
Permalink
Post by f***@gmail.com
hello i immediately need help!!!
i am trying to learn overloading and i am in 10 standard so please help me
i am getting identifier expected error
There is usually also a line number in the compilation error message,
pointing to a specific line in your code. See which line it points to.
(The actual error may in some cases be somewhere before the line where
the compiler realized something is wrong.)
--
Jukka Lahtinen
Eric Sosman
2018-06-23 12:05:57 UTC
Permalink
Post by f***@gmail.com
hello i immediately need help!!!
i am trying to learn overloading and i am in 10 standard so please help me
i am getting identifier expected error
import jav.io.*;
"jav"?
Post by f***@gmail.com
public class Overload
{
double r,side,l,b,AreaSquare,AreaCircle,AreaRectangle;
public void area(side)//this the line where error occurs
You must tell Java what type `side' is: a `double', an `int',
or whatever. Thus, for example,

public void area(double side) // if `side' is a `double'
Post by f***@gmail.com
{
side1=side;
Similarly, you must specify the type of `side1'. Java cannot
just guess whether it should be a `double' or `float' or `long',
you must write what you intend. (You've made the same omission
several more times; I won't point them all out but will trust you
to fix them yourself.)
Post by f***@gmail.com
AreaSquare=side1*side1;
System.out.println("AREA OF SQUARE="+Areasquare);
}
public void area(l,b)
{
l1=l;
b1=b;
AreaRectangle=l1*b1;
System.out.println("AREA OF RECTENGLE="+AreaRectangle);
}
public void area(r)
I anticipate trouble with this overload, because if you decide
to make `r' a `double' and you have also made `side' a `double'
above, then Java will have no way to distinguish the two methods:
They will have identical parameter lists. (The names of parameters
don't disambiguate, only their number and types matter.)
Post by f***@gmail.com
{
r1=r;
PI=3.14;
FYI, the predefined constant `Math.PI' is considerably more
accurate.
Post by f***@gmail.com
AreaCircle=PI*r1*r1;
System.out.println("AREA OF CIRCLE="+AreaCircle);
}
public static void main()throws IOException
{
BufferedReader in=new BufferedReader (new InputStreamReader(System.in));
Overload obj=new Overload();
System.out.println("ENTER THE SIDE OF SQUARE=");
side1=Double.parseDouble(in.readLine());
System.out.println("ENTER THE LENGTH OF RECTANGLE=");
l1=Double.parseDouble(in.readLine());
System.out.println("ENTER THE BREADTH OF RECTANGLE=");
b1=Double.parseDouble(in.readLine());
System.out.println("ENTER THE RADIUS OF CIRCLE=");
r1=Double.parseDouble(in.readLine());
obj.area(side);
obj.area(l,b);
obj.area(r);
}
}
--
***@comcast-dot-net.invalid
Nine hundred forty-two days to go.
f***@gmail.com
2018-06-23 16:03:14 UTC
Permalink
Post by Eric Sosman
Post by f***@gmail.com
hello i immediately need help!!!
i am trying to learn overloading and i am in 10 standard so please help me
i am getting identifier expected error
import jav.io.*;
"jav"?
Post by f***@gmail.com
public class Overload
{
double r,side,l,b,AreaSquare,AreaCircle,AreaRectangle;
public void area(side)//this the line where error occurs
You must tell Java what type `side' is: a `double', an `int',
or whatever. Thus, for example,
public void area(double side) // if `side' is a `double'
Post by f***@gmail.com
{
side1=side;
Similarly, you must specify the type of `side1'. Java cannot
just guess whether it should be a `double' or `float' or `long',
you must write what you intend. (You've made the same omission
several more times; I won't point them all out but will trust you
to fix them yourself.)
Post by f***@gmail.com
AreaSquare=side1*side1;
System.out.println("AREA OF SQUARE="+Areasquare);
}
public void area(l,b)
{
l1=l;
b1=b;
AreaRectangle=l1*b1;
System.out.println("AREA OF RECTENGLE="+AreaRectangle);
}
public void area(r)
I anticipate trouble with this overload, because if you decide
to make `r' a `double' and you have also made `side' a `double'
They will have identical parameter lists. (The names of parameters
don't disambiguate, only their number and types matter.)
Post by f***@gmail.com
{
r1=r;
PI=3.14;
FYI, the predefined constant `Math.PI' is considerably more
accurate.
Post by f***@gmail.com
AreaCircle=PI*r1*r1;
System.out.println("AREA OF CIRCLE="+AreaCircle);
}
public static void main()throws IOException
{
BufferedReader in=new BufferedReader (new InputStreamReader(System.in));
Overload obj=new Overload();
System.out.println("ENTER THE SIDE OF SQUARE=");
side1=Double.parseDouble(in.readLine());
System.out.println("ENTER THE LENGTH OF RECTANGLE=");
l1=Double.parseDouble(in.readLine());
System.out.println("ENTER THE BREADTH OF RECTANGLE=");
b1=Double.parseDouble(in.readLine());
System.out.println("ENTER THE RADIUS OF CIRCLE=");
r1=Double.parseDouble(in.readLine());
obj.area(side);
obj.area(l,b);
obj.area(r);
}
}
--
Nine hundred forty-two days to go.
thanks very much!!!
my program is working!!!
i will always be grateful to you!!!
you helped me to improve!!!
thanks!!! thanks!!!
thanks!!!

Loading...