-1

We know some of this syntax won't compile, specifically the line where I'm explicitly referring to the class name for a variable.

class Main {

    static String Main.s1 ="output"; //won't compile
    static String s2 = "output";

    public static void main(String... s1) {
        System.out.println("output");
    }
}

The Main.s1 ="output"; gives a compiler error. But we do know that the following is ok:

class Main {

    static String s2 = "output";

    public static void main(String... s1) {
        System.out.println(Main.s2);
    }
}

Similarly, the following is also ok.

class Main {

    static String s2 = "output";

    public static void main(String... s1) {
        Main.s2 = "baz";
        System.out.println(Main.s2);
    }
}

It's just at declaration that the compilers complains. I read that there are these rules for identifiers in Java

  • reserved words cannot be used. they cannot start with a digit but
  • digits can be used after the first character (e.g., name1, n2ame are valid). they can start with a letter, an underscore (i.e., "_") or a dollar sign (i.e., "$").
  • you cannot use other symbols or spaces (e.g., "%","^","&","#").

So apparently it is not legal to use a dot in an identifier. I agree is it unnecessary, but there is a lot of other unnecessary legal code.

Any reason why the dot is illegal in a Java identifier?

  • Don't your second and third examples show why the first is not allowed; it would make it really ambiguous where the variable name starts – Richard Tingle Jan 11 '17 at 8:30
  • 2
    The dot . is illegal in variable names for the same reason you may not use +, |, or [ in a variable: they are all operators. In particular, the dot is both the namespace traversal operator (looks up a declaration within a package or class) and also the field access operator (look up a field or method in an object instance). – amon Jan 11 '17 at 8:45
  • Main.s2 = "baz"; In this line the dot is not part of an identifier, but serves to disambiguate which 's2' you are referring to. If you had written s2 = "baz";, for example, you may have been referring to a local variable. – Brandin Jan 11 '17 at 9:11
  • Pretty much the same reason you can't have + in an identifier. – immibis Jan 11 '17 at 10:18
3

The dot is used to denote the end of one identifier and the start of a sub-item of that identifier:

package1.package2.class.innerclass.method

When the compiler reaches each ., it knows it's reached the end of one identifier and is moving on to the next. If dots were allowed in names, then the method might actually be called innerclass.method. If you then also have a class called class.innerclass in package1.package2 and a class called innerclass in class, then how is the compiler to resolve what it is that you are referring to?

  • "When the compiler reaches each '.', it knows it's reached the end of one identifier." - May not be technically accurate. Consider ellipsis usage: void f(T... x). The first dot may be considered the end of 'T'. but the second and third dots do not signal the end of separate identifiers. – Brandin Jan 11 '17 at 9:18
3

The reason is demonstrated in your second two examples, it would make the code ambiguous for no real value (how often does it really make sense to put a dot in a variable name)

Take this line

int b = Main.a

It is quite clear under real java what this means; class Main has a field a and you'd like to set it's value to b.

Under your system it might mean that or there might be a local variable called Main.a. They may both exist!

So basically; you can't use the dot because java is already using it (you could make more of an argument for some of the other symbols but it's never a bad idea to reserve some symbols you "might use", especially given java's focus on backwards compatability)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.