Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

compilation error : identifier expected  RSS feed

 
raymond hsu
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

when I compile a very simple java program as follows :

import java.awt.*;
import java.awt.event.*;

public class addinpint extends ActionListener {
Integer accutot =0;
Frame f = new Frame();
f.setLocation(10, 10 );
f.setSize(300,100);
}

I got the following compilation errors, not sure what that means, can anyone help? thanks.

C:\DOCUME~1\RAY>cd c:\javapr

C:\JAVAPR>javac addinpint.java
addinpint.java:7: <identifier> expected
f.setLocation(10, 10 );
^
addinpint.java:7: illegal start of type
f.setLocation(10, 10 );
^
addinpint.java:7: illegal start of type
f.setLocation(10, 10 );
^
addinpint.java:8: <identifier> expected
f.setSize(300,100);
^
addinpint.java:8: illegal start of type
f.setSize(300,100);
^
addinpint.java:8: illegal start of type
f.setSize(300,100);
^
6 errors
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My comments are in your code block, please see them.

Your Class also implements ActionListener, so you will have to implement the ActionPerformed method before it will compile correctly--see the ActionListener API to see specifics.
raymond hsu wrote:hello,

when I compile a very simple java program as follows :
 
raymond hsu
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Morgan for your reply.
I removed the extend ActionListener from the code, just to isolate potential problem from the ActionListener

the code is as below :

public class addinpint {
Integer accutot =0;
Frame f = new Frame();
f.setLocation(10, 10 );
f.setSize(300,100);
}

but I still get the same set of 6 errors after compilation.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are putting statements directly inside a class declaration. That's not how Java works. These statements need to be inside a constructor or a method.

For example (this is not useful code, just to illustrate the point):

 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read my comments in your code, and as Jesper said: you cannot have code, that is not Class or instance variable declaration and initialization, inside your Class body.
raymond hsu wrote:Thanks Morgan for your reply.
I removed the extend ActionListener from the code, just to isolate potential problem from the ActionListener
but I still get the same set of 6 errors after compilation.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raymond hsu wrote:. . . I removed the extend ActionListener from the code, just to isolate potential problem from the ActionListener . . .
Quite right. Don't make a display class implement action listeners; it is very bad design, even if you can find it in many books.
Also stop using AWT and stop using Frame. Use JFrame instead. Nobody has used AWT for ten years. In fact I think in a few years nobody will use Swing any more; they will use JavaFX.
 
raymond hsu
Greenhorn
Posts: 23
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Jesper,

it is another of my silly mistake, without creating a constructor or method to house the statements,
now no more of those 6 compilation errors.

I will take note of the suggestion of AWT will soon be obsolete.
thanks for the suggestion.

I am just trying to pick up Java from the very fundamentals and basic, that is eay to learn.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!