Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

<indentifier> expected

 
Rob Sweeny
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Happy to find a newbe friendly forum!

I've got my code to be real simple but it still is giving me problems.

I'm trying to create and use a class outside of my main class.

Main Class: GreetingsUniverse

package com.scjaexam.tutorial;
//import com.scjaexam.tutorial.planets.*;

public class GreetingsUniverse {
public static void main(String[] args) {
System.out.println("Greetings, Universe!");
Earth e = new Earth();
Mars e = new Mars();
Veuns e = new Venus();

}
}

Other Class: Earth

package com.sjcaexam.tutorial;

public class Earth {
public Earth {
System.out.println("Hello from Earth");
}
}


javac GreetingsUniverse.java

results in:

.\com\scjaexam\tutorial\Earth.java:4: <identifier> expected
public Earth {
^


Any help would be greatly appreciated.
Thanks
Rob
 
W. Joe Smith
Ranch Hand
Posts: 710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try:




Also, when you post code, please use code tags. You can find the Code button along the top of the box where you post your message body. Thanks!
 
Rob Sweeny
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Main Class: GreetingsUniverse



Other Class: Earth



javac GreetingsUniverse.java

results in:

.\com\scjaexam\tutorial\Earth.java:4: <identifier> expected
public Earth {
_________^


Any help would be greatly appreciated.
Thanks
Rob
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You saw that the question was answered, correct? (Although normally we try to provide hints rather than absolute solutions.)
 
Rob Sweeny
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick responses!

Now I have the same error on a different line

Could it have something to do with packaging?


Main Class: GreetingsUniverse



Other Class: Earth



c:\code>javac GreetingsUniverse.java
.\com\scjaexam\tutorial\Earth.java:4: <identifier> expected
System.out.println("Hello from Earth");
______________^

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope. Now you're trying to write non-initialization code outside of any method.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:You saw that the question was answered, correct? (Although normally we try to provide hints rather than absolute solutions.)


You're right. That was my mistake.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@W Joe Smith: No problem; just a handy reminder ;) I have a towel signed by Douglas Adams.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Newton wrote:@W Joe Smith: No problem; just a handy reminder ;) I have a towel signed by Douglas Adams.


Bah! I wish I had that.....
 
Campbell Ritchie
Sheriff
Pie
Posts: 47216
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch
 
Rob Sweeny
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to JavaRanch


Happy to be here.

i found the problem.


Original code:



created <identifier> expected error

Fixed Code:



the identifier that was expected was () to make this code into a proper constructor.

Thanks Everyone
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, no, the "proper identifier" is the method, not the empty parameter list.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic