• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"identifier expected" error

 
Joe Jones
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I'm new to java and would like some help figuring out what I'm doing wrong. Here's my code:
-----------
package testpackage;

public class Class1 {

public String firstMethod() {
return "My java class!";
}
}
-----------
package testpackage;

public class Class2 {

Class1 c = new Class1();

System.out.println(c.firstMethod());
}
-----------

Problem is, when Class2 is compiled I'm seeing the following error:
... Class2.java:7: <identifier> expected

Can someone tell me how I can correct this?

Thanks!
Greg K.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can list a method call like that outside of a method or initializer block. Try putting braces around the line.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or maybe you wanted to put it in the main() method ?


[ March 02, 2006: Message edited by: Satou kurinosuke ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Greg K.,"

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your name here.

Thank you for your prompt attention, and enjoy the ranch!

-Marc
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic