1

Hi I was creating simple program and got unseen compilation error in commented code.My code is as below :

public class Static_Method_Call
{               
    public static Character character=getMe();

    public static void main(String[] args)
    {
        System.out.println("Inside main() 1 : "+character); 
        //Static_Method_Call.character=new Character('\u000d'); 
        //System.out.println("Inside main() 2 : "+character);
    }

    static
    {
        System.out.println("Inside static block : "+character);
        Static_Method_Call.character=new Character('\u003d');       
    }

    public static Character getMe()
    {
        System.out.println("Inside getMe() : "+character);
        return new Character('\u002d');
    }
}

Error is as below :

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Invalid character constant

What does this error mean in Java ?

Ravi Jiyani
  • 919
  • 1
  • 10
  • 26
  • You are giving the character the value of a method. Which is not possible but there are soms more errors in your program . – Markvds Feb 14 '15 at 11:46
  • @user1758777 No, OP is assigning the variable `character` to the return value of the static method `getMe()` which is perfectly valid. – initramfs Feb 14 '15 at 11:52
  • look out here http://stackoverflow.com/questions/8115522/a-unicode-newline-character-u000d-in-java – Anil Feb 14 '15 at 11:56
  • My bad. Read the code wrong ignore my comment – Markvds Feb 14 '15 at 11:57

2 Answers2

7

\u000d is a Unicode character that stands for the CR special character. Even before the compiler transforms the source code, this character is pre-processed and causes the source code to be invalid. So I guess at pre-processing, the commented line would look something like:

//Static_Method_Call.character=new Character('
 ');

Hence the compiler error. You can use \r to add a carriage return.

M A
  • 71,713
  • 13
  • 134
  • 174
1

\u000d is a newline character, so next line is starting with ' which is unclosed that is what it is complaining. this is explained here A unicode newline character(\u000d) in Java

Community
  • 1
  • 1
Anil
  • 578
  • 4
  • 12
  • 1
    `\u000d` is a carriage return but anyway it doesn't make a difference here. See http://en.wikipedia.org/wiki/List_of_Unicode_characters#Control_codes. – M A Feb 14 '15 at 12:10