• Post Reply Bookmark Topic Watch Topic
  • New Topic

Compiler Error  RSS feed

 
Swapna latha
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


D:\surya>javac Test21.java
Test21.java:6: 'class' or 'interface' expected
package test2;
^
1 error

Why i am getting this error ? I am not understanding this.

Thanks & Regards
Kori Swapna Latha

[Edit - fixed code tags and formatting - MB]
 
Korhan Rankin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Swapna,

you have a scope/compile problem, x is not accessible for Test21.

public class Test1
{
public static int x=42;
}

better:
public class Test1
{
protected static int x=42;
}

fixes it.

public = everyone can access it.
protected = only class hierarchy can access it. reduce visibilty as much as possible.

I'm not sure if that fixes your actual compile problem. Give it a try.. you should get eclipse, much easier to learn java.

cheers
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Swapna latha wrote:Test21.java:6: 'class' or 'interface' expected
package test2;
^
1 error

Why i am getting this error ? I am not understanding this.

You can only have one package declaration in a .java file, because a class can only belong to one package; and it must be the first statement other than comments.

Also: you can only have one public class in a source file; and it must have the same name as the file without the '.java'.

Winston
 
Korhan Rankin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Swapna latha wrote:Test21.java:6: 'class' or 'interface' expected
package test2;
^
1 error

Why i am getting this error ? I am not understanding this.

You can only have one package declaration in a .java file, because a class can only belong to one package; and it must be the first statement other than comments.

Also: you can only have one public class in a source file; and it must have the same name as the file without the '.java'.

Winston


I acutally didn't thought that the mentioned code is in one file.. if so Winston is right of course.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Korhan Rankin wrote:you should get eclipse, much easier to learn java.

I disagree. Much better that Swapna make these mistakes now, and learn to use Java properly from the command line. The time for an IDE is when you start building projects for real, not while you're learning.

Winston
 
Korhan Rankin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Korhan Rankin wrote:you should get eclipse, much easier to learn java.

I disagree. Much better that Swapna make these mistakes now, and learn to use Java properly from the command line. The time for an IDE is when you start building projects for real, not while you're learning.

Winston


I agree with your point, learning Java properly is better with starting from command line.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!