3

I have a simple class with just a main method that prints something and a package statement as follows

package default;

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

        System.out.printf("something\n");
    }
}

I have put the file Main.java into a folder default and I have attempted to compile it using the following command.

javac default/Main.java

The problem is that when I try to compile the class I get the following error message verbatim

default/Main.java:1: error: <identifier> expected
package default;
       ^
1 error

I'm really confused by this. If I take the package statement out or put the code into an IDE such as eclipse, the error goes away and it works fine. I have tried scouring the internet for answers to this question and all sources say that what I'm doing should work.

I have also tried to modify the compile statement as recommended by several Stack Overflow threads by including a classpath as follows

javac -cp default default/Main.java

but the error message remains the same.

Some of my colleagues at my university have suggested that using packages outside of an IDE is impossible and that the only way for my code to compile by command line is to remove the package statement, but that answer doesn't seem right to me.

If anyone knows why I am having this issue, I would really appreciate a response. I just really want to know what I am doing wrong.

4
  • You can't have a package which has the same name as a keyword as you can see from the code highlighting that it's a keyword, not an allowed identifier. Commented Oct 19, 2016 at 18:39
  • Oh man. Thanks a lot. I feel...not smart after that one. I had no idea default was a keyword. Commented Oct 19, 2016 at 18:55
  • @Wesley: check switch statements, the you'll find default. Commented Oct 19, 2016 at 18:57
  • "feel ... not smart" means you learnt something. ;) Happens to every one. Commented Oct 19, 2016 at 19:09

1 Answer 1

1

"default", Is a keyword in java, you can't use keywords as package name or for example as a variable name. The default keyword belongs to a switch statement:

switch(x){
    case 1: 
        //do stuff if x is 1...
        break;
    default:
        //do stuff if x is not any of the other cases
        break;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.