Reserved words as variable or method names












19















Is there any tricky way to use Java reserved words as variable or method names?










share|improve this question




















  • 4





    just don't do it

    – inspite
    Jan 8 '09 at 11:49






  • 5





    It's not so strange, that he want do that. That's why C# has @ prefix, and Delphi has & and variable or method named eg. @if is legal in C#.

    – prostynick
    Sep 3 '10 at 8:04






  • 5





    I don't get all the "don't do it" comments. It is perfectly legitimate to call APIs from Java that weren't themselves written in Java, and there are identifiers which are perfectly legal in JVM byte code which aren't legal in Java. That's precisely why languages like C# (@class), Scala (`class`) and others have escaping mechanisms. There are 500 languages on the JVM, if I tried to take every single reserved word from every single one of those 500 languages into account when designing APIs, I'd go insane (and someone might invent a new language with a new reserved word later anyway).

    – Jörg W Mittag
    Aug 25 '15 at 22:05


















19















Is there any tricky way to use Java reserved words as variable or method names?










share|improve this question




















  • 4





    just don't do it

    – inspite
    Jan 8 '09 at 11:49






  • 5





    It's not so strange, that he want do that. That's why C# has @ prefix, and Delphi has & and variable or method named eg. @if is legal in C#.

    – prostynick
    Sep 3 '10 at 8:04






  • 5





    I don't get all the "don't do it" comments. It is perfectly legitimate to call APIs from Java that weren't themselves written in Java, and there are identifiers which are perfectly legal in JVM byte code which aren't legal in Java. That's precisely why languages like C# (@class), Scala (`class`) and others have escaping mechanisms. There are 500 languages on the JVM, if I tried to take every single reserved word from every single one of those 500 languages into account when designing APIs, I'd go insane (and someone might invent a new language with a new reserved word later anyway).

    – Jörg W Mittag
    Aug 25 '15 at 22:05
















19












19








19


1






Is there any tricky way to use Java reserved words as variable or method names?










share|improve this question
















Is there any tricky way to use Java reserved words as variable or method names?







java naming reserved-words






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 14 '09 at 20:23









Vlad Gudim

16.7k146391




16.7k146391










asked Jan 8 '09 at 11:47























  • 4





    just don't do it

    – inspite
    Jan 8 '09 at 11:49






  • 5





    It's not so strange, that he want do that. That's why C# has @ prefix, and Delphi has & and variable or method named eg. @if is legal in C#.

    – prostynick
    Sep 3 '10 at 8:04






  • 5





    I don't get all the "don't do it" comments. It is perfectly legitimate to call APIs from Java that weren't themselves written in Java, and there are identifiers which are perfectly legal in JVM byte code which aren't legal in Java. That's precisely why languages like C# (@class), Scala (`class`) and others have escaping mechanisms. There are 500 languages on the JVM, if I tried to take every single reserved word from every single one of those 500 languages into account when designing APIs, I'd go insane (and someone might invent a new language with a new reserved word later anyway).

    – Jörg W Mittag
    Aug 25 '15 at 22:05
















  • 4





    just don't do it

    – inspite
    Jan 8 '09 at 11:49






  • 5





    It's not so strange, that he want do that. That's why C# has @ prefix, and Delphi has & and variable or method named eg. @if is legal in C#.

    – prostynick
    Sep 3 '10 at 8:04






  • 5





    I don't get all the "don't do it" comments. It is perfectly legitimate to call APIs from Java that weren't themselves written in Java, and there are identifiers which are perfectly legal in JVM byte code which aren't legal in Java. That's precisely why languages like C# (@class), Scala (`class`) and others have escaping mechanisms. There are 500 languages on the JVM, if I tried to take every single reserved word from every single one of those 500 languages into account when designing APIs, I'd go insane (and someone might invent a new language with a new reserved word later anyway).

    – Jörg W Mittag
    Aug 25 '15 at 22:05










4




4





just don't do it

– inspite
Jan 8 '09 at 11:49





just don't do it

– inspite
Jan 8 '09 at 11:49




5




5





It's not so strange, that he want do that. That's why C# has @ prefix, and Delphi has & and variable or method named eg. @if is legal in C#.

– prostynick
Sep 3 '10 at 8:04





It's not so strange, that he want do that. That's why C# has @ prefix, and Delphi has & and variable or method named eg. @if is legal in C#.

– prostynick
Sep 3 '10 at 8:04




5




5





I don't get all the "don't do it" comments. It is perfectly legitimate to call APIs from Java that weren't themselves written in Java, and there are identifiers which are perfectly legal in JVM byte code which aren't legal in Java. That's precisely why languages like C# (@class), Scala (`class`) and others have escaping mechanisms. There are 500 languages on the JVM, if I tried to take every single reserved word from every single one of those 500 languages into account when designing APIs, I'd go insane (and someone might invent a new language with a new reserved word later anyway).

– Jörg W Mittag
Aug 25 '15 at 22:05







I don't get all the "don't do it" comments. It is perfectly legitimate to call APIs from Java that weren't themselves written in Java, and there are identifiers which are perfectly legal in JVM byte code which aren't legal in Java. That's precisely why languages like C# (@class), Scala (`class`) and others have escaping mechanisms. There are 500 languages on the JVM, if I tried to take every single reserved word from every single one of those 500 languages into account when designing APIs, I'd go insane (and someone might invent a new language with a new reserved word later anyway).

– Jörg W Mittag
Aug 25 '15 at 22:05














14 Answers
14






active

oldest

votes


















25














No, there is no way. That's why they're labeled "reserved".






share|improve this answer



















  • 3





    Adding a " _ " in front of a reserved keyword like "_new" is the closest thing you'll get.

    – NoName
    Nov 3 '17 at 1:27



















34














This is a valid question. Such a thing is possible in other languages. In C#, prefix the identifier with @ (as asked before); in Delphi, prefix with &. But Java offers no such feature (partly because it doesn't really need to interact with identifiers defined by other languages the way the .Net world does).






share|improve this answer


























  • Well, if you are willing to accept the C# hack, you can prefix Java names with ZZZ or any other character sequence that you won't otherwise use as a prefix.

    – Ira Baxter
    Nov 8 '09 at 20:55






  • 8





    But Ira, that changes the identifier. The C# and Delphi ways escape the original name, but it's still the same name. In Delphi, you can even write the fully qualified name without the escape character.

    – Rob Kennedy
    Nov 8 '09 at 21:01






  • 1





    This becomes especially visible in Xml or JSON serialization as the serialized text would have ZZZ as part of the tag name <ZZZstring></ZZZstring>, whereas in C#, @string would serialize to <string></string>.

    – Rhyous
    Mar 31 '16 at 22:21



















11














Most often this issue comes up for "class", in this case it is customary to write "clazz".






share|improve this answer



















  • 4





    Is there a best practices alternatives list for all keywords?

    – Ciro Santilli 新疆改造中心 六四事件 法轮功
    Apr 9 '15 at 9:23





















6














No, you can't do this. For more information please go to JLS Sections 3.8, 3.9




The following character sequences,
formed from ASCII letters, are
reserved for use as keywords and
cannot be used as identifiers (§3.8):



Keyword: one of
abstract continue for new switch
assert default if package synchronized
boolean do goto private this
break double implements protected throw
byte else import public throws
case enum instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp volatile
const float native super while






share|improve this answer



















  • 2





    shame about super

    – inspite
    Jan 8 '09 at 12:07



















6














Strictly speaking you can't, unless you get your hands on a buggy compiler implementation that doesn't adhere to the Java language spec.



But where there's a will, there's a way. Copy the following code into your IDE, switch the source file encoding to UTF-16 and here we go:



public class HelloWorld {

public static void main(String args) {

HelloWorld.nеw();
}

public static void nеw () {
System.out.println("Hello,World");
}

}


This code is a well-formed and valid Java class. However, as you have guessed there is a little trick: the 'е' character within "new" identifier does not belong to the ASCII character set, it is actually a cyrrilic 'е' (prounanced 'YE').



Current Java language spec explicitly permits, and this an important point to make, the use of Unicode for naming identifiers. That means that one has an ability to freely call her or his classes in French, Chinise or Russian if they wish. It is also possible to mix and match the alphabets within code. And historically, some letters within Latin and other alphabets are lookalikes.



As a result: no, you can't use the reserved words as identifiers, but you can use identifiers that look exactly like reserved words.



Whether anyone should be doing it is a totally different matter.






share|improve this answer





















  • 2





    Down voted because these kinds of tricks can lead to bugs and behavior that is not easily understood. Why not name it 'n3w' or anything else as it is intended to change the behavior.

    – CodeMonkeyKing
    Jul 13 '15 at 23:14






  • 1





    @CodeMonkeyKing, unfortunately not everyone and not in every scenario follows best naming conventions and it is entirely possible that someone might come across a piece of code that included an identifier looking exactly as a reserved word and, perplexingly, compiled without errors. This answer would come to the rescue then. Besides it is answering the actual question being asked. "Whether anyone should be doing it is a totally different matter."

    – Vlad Gudim
    Aug 4 '15 at 13:48






  • 3





    If I parse the OP's question exactly the answer you suggest only appears to the human eye to be a keyword and is not a "tricky way to use a Java reserved word". I would say from experience that these are the most difficult bugs to find. The real answer to this question is that reserved keywords are in fact reserved.

    – CodeMonkeyKing
    Aug 4 '15 at 18:04






  • 1





    I feel like this answer deserves a "Don't try this at home (or in the workplace)" label.

    – Chris Kerekes
    May 26 '17 at 17:42





















4














Yes, there is.
You have to use reserved words from the future.
Like what happened with different methods called assert() in pre-1.4 code.



Hope it helps!






share|improve this answer































    3














    Huh? Why would you want to do that? You can write them in l33t, that will fool the compiler.



    class cl4ss {
    String r3turn() {
    return "but why?";
    }
    }





    share|improve this answer



















    • 3





      Technically - you never fool the compiler as the above example is completely legal Java.

      – CodeMonkeyKing
      Aug 4 '15 at 18:11



















    3














    I know it's old question still, might help someone.



    It's possible by using GSON's Field Naming Support



    eg.



    @SerializedName("new")
    private String New;
    public String getNew ()
    {
    return New;
    }
    public void setNew (String aNew)
    {
    New = aNew;
    }





    share|improve this answer





















    • 1





      It helped me, thanks

      – Jake Gaston
      Mar 2 '18 at 16:21











    • Glad to be of help @jake gaston It's workaround i needed at one Project which already had defined variables which couldn't be changed.

      – SK16
      Mar 3 '18 at 6:39





















    2














    It's bad enough some case-sensitive languages allow things like the following:



    class New;

    class Something
    {
    New makeNew()
    {
    return new New();
    }
    }


    But why would you ever want to be able to write a line of code like this:



    class new;

    class Something
    {
    bool if;

    new makeNew()
    {
    return if ? new new() : null;
    }
    }


    Just take a look at the syntax highlighting. Even it gets confused!






    share|improve this answer



















    • 4





      You can ridicule any feature by understanding its potential applications wrong or by giving bad examples. That doesn't imply that better uses don't exist. Sometimes, a reserved word in indeed the most natural name (as is often the case when the identifier clazz oder klass is used).

      – Konrad Rudolph
      Jan 14 '09 at 20:31











    • I don't think what I supposed was a 'bad example'. A bit over the top, yes, but not bad. Maybe the most natural name is a reserved word, is it good to use that name? Reserved words are reserved for a reason. It's easy to distinguish 'class' and 'clazz'. Can you distinguish 'class' and 'class'?

      – lc.
      Jan 15 '09 at 7:00






    • 3





      But not all languages agree what the reserved words are. In Scala, it is perfectly natural, to name a method that returns the default value default. This works perfectly fine, because default is not a reserved word in Scala. It is, however, a reserved word in Java, thus, you cannot call this method from Java, even though you have perfectly legitimate reason to do so.

      – Jörg W Mittag
      Aug 25 '15 at 22:11



















    2














    There is no way to use reserved words with the javac compiler.



    Technically, you can edit the names inside the class file once it's compiled to be anything you want: at that stage, the VM doesn't care, because it's not dealing with source code any more. I believe some obfuscators use this technique.






    share|improve this answer































      2














      PL/1 (a 1960's IBM mainframe programming language still around today) rather famously required that while some words act like keywords in certain contexts, all words can be used as identifiers. This isn't even that hard to do in a parser if you set out to be consistent about it. PL/1 was considered to a rather big langauge, and the langauge committee worried that many programmers wouldn't learn all of it, and then would get suprised when they tried to use the keyword from a part they didn't know as an identifier.
      So you could write things like:



      IF BEGIN=ELSE THEN CALL=3 ELSE CALL FOO(ENDIF) ENDIF


      As others have noted here, the ability to do this isn't a recommendation.



      The Java designers decided the number of keywords in the langauge was modest, and reserved the set. They even reserved 'GOTO', which isn't actually allowed in any real Java program.






      share|improve this answer































        2














        Not sure what you're trying to do, but $ is a valid character in identifiers, so you could do, say:



        int $return = 5;



        It looks a little weird, but it does work.






        share|improve this answer





















        • 1





          This is similar to @identifier in C#, although it doesn't end in the same result (in C# the '@' does not become part of the identifier)

          – Hosam Aly
          Jan 24 '09 at 8:38



















        1














        In Scala you can use backticks. For example: myVarialbe.`class`






        share|improve this answer































          0














          If you really need to use a field/local variable/method named the same as a reserved word, I suggest appending an underscore at the end of the name:



          // JPA entity mapping class:

          private Boolean void_;

          public Boolean getVoid_() { ... }
          void setVoid_(Boolean void_) { ... }


          It is a more readable choice (IMHO) than appending chars at the beginning of the name (fVoid, aVoid, vVoid, etc.)



          The code above is a real world case that happened to me, working with a legacy database, in which the invoice table had a field named void indicating whether the document had been voided or not.






          share|improve this answer

























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f423994%2freserved-words-as-variable-or-method-names%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown
























            14 Answers
            14






            active

            oldest

            votes








            14 Answers
            14






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            25














            No, there is no way. That's why they're labeled "reserved".






            share|improve this answer



















            • 3





              Adding a " _ " in front of a reserved keyword like "_new" is the closest thing you'll get.

              – NoName
              Nov 3 '17 at 1:27
















            25














            No, there is no way. That's why they're labeled "reserved".






            share|improve this answer



















            • 3





              Adding a " _ " in front of a reserved keyword like "_new" is the closest thing you'll get.

              – NoName
              Nov 3 '17 at 1:27














            25












            25








            25







            No, there is no way. That's why they're labeled "reserved".






            share|improve this answer













            No, there is no way. That's why they're labeled "reserved".







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 8 '09 at 11:49









            SaltySalty

            5,70432829




            5,70432829








            • 3





              Adding a " _ " in front of a reserved keyword like "_new" is the closest thing you'll get.

              – NoName
              Nov 3 '17 at 1:27














            • 3





              Adding a " _ " in front of a reserved keyword like "_new" is the closest thing you'll get.

              – NoName
              Nov 3 '17 at 1:27








            3




            3





            Adding a " _ " in front of a reserved keyword like "_new" is the closest thing you'll get.

            – NoName
            Nov 3 '17 at 1:27





            Adding a " _ " in front of a reserved keyword like "_new" is the closest thing you'll get.

            – NoName
            Nov 3 '17 at 1:27













            34














            This is a valid question. Such a thing is possible in other languages. In C#, prefix the identifier with @ (as asked before); in Delphi, prefix with &. But Java offers no such feature (partly because it doesn't really need to interact with identifiers defined by other languages the way the .Net world does).






            share|improve this answer


























            • Well, if you are willing to accept the C# hack, you can prefix Java names with ZZZ or any other character sequence that you won't otherwise use as a prefix.

              – Ira Baxter
              Nov 8 '09 at 20:55






            • 8





              But Ira, that changes the identifier. The C# and Delphi ways escape the original name, but it's still the same name. In Delphi, you can even write the fully qualified name without the escape character.

              – Rob Kennedy
              Nov 8 '09 at 21:01






            • 1





              This becomes especially visible in Xml or JSON serialization as the serialized text would have ZZZ as part of the tag name <ZZZstring></ZZZstring>, whereas in C#, @string would serialize to <string></string>.

              – Rhyous
              Mar 31 '16 at 22:21
















            34














            This is a valid question. Such a thing is possible in other languages. In C#, prefix the identifier with @ (as asked before); in Delphi, prefix with &. But Java offers no such feature (partly because it doesn't really need to interact with identifiers defined by other languages the way the .Net world does).






            share|improve this answer


























            • Well, if you are willing to accept the C# hack, you can prefix Java names with ZZZ or any other character sequence that you won't otherwise use as a prefix.

              – Ira Baxter
              Nov 8 '09 at 20:55






            • 8





              But Ira, that changes the identifier. The C# and Delphi ways escape the original name, but it's still the same name. In Delphi, you can even write the fully qualified name without the escape character.

              – Rob Kennedy
              Nov 8 '09 at 21:01






            • 1





              This becomes especially visible in Xml or JSON serialization as the serialized text would have ZZZ as part of the tag name <ZZZstring></ZZZstring>, whereas in C#, @string would serialize to <string></string>.

              – Rhyous
              Mar 31 '16 at 22:21














            34












            34








            34







            This is a valid question. Such a thing is possible in other languages. In C#, prefix the identifier with @ (as asked before); in Delphi, prefix with &. But Java offers no such feature (partly because it doesn't really need to interact with identifiers defined by other languages the way the .Net world does).






            share|improve this answer















            This is a valid question. Such a thing is possible in other languages. In C#, prefix the identifier with @ (as asked before); in Delphi, prefix with &. But Java offers no such feature (partly because it doesn't really need to interact with identifiers defined by other languages the way the .Net world does).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 23 '17 at 11:47









            Community

            11




            11










            answered Jan 8 '09 at 12:48









            Rob KennedyRob Kennedy

            145k16230407




            145k16230407













            • Well, if you are willing to accept the C# hack, you can prefix Java names with ZZZ or any other character sequence that you won't otherwise use as a prefix.

              – Ira Baxter
              Nov 8 '09 at 20:55






            • 8





              But Ira, that changes the identifier. The C# and Delphi ways escape the original name, but it's still the same name. In Delphi, you can even write the fully qualified name without the escape character.

              – Rob Kennedy
              Nov 8 '09 at 21:01






            • 1





              This becomes especially visible in Xml or JSON serialization as the serialized text would have ZZZ as part of the tag name <ZZZstring></ZZZstring>, whereas in C#, @string would serialize to <string></string>.

              – Rhyous
              Mar 31 '16 at 22:21



















            • Well, if you are willing to accept the C# hack, you can prefix Java names with ZZZ or any other character sequence that you won't otherwise use as a prefix.

              – Ira Baxter
              Nov 8 '09 at 20:55






            • 8





              But Ira, that changes the identifier. The C# and Delphi ways escape the original name, but it's still the same name. In Delphi, you can even write the fully qualified name without the escape character.

              – Rob Kennedy
              Nov 8 '09 at 21:01






            • 1





              This becomes especially visible in Xml or JSON serialization as the serialized text would have ZZZ as part of the tag name <ZZZstring></ZZZstring>, whereas in C#, @string would serialize to <string></string>.

              – Rhyous
              Mar 31 '16 at 22:21

















            Well, if you are willing to accept the C# hack, you can prefix Java names with ZZZ or any other character sequence that you won't otherwise use as a prefix.

            – Ira Baxter
            Nov 8 '09 at 20:55





            Well, if you are willing to accept the C# hack, you can prefix Java names with ZZZ or any other character sequence that you won't otherwise use as a prefix.

            – Ira Baxter
            Nov 8 '09 at 20:55




            8




            8





            But Ira, that changes the identifier. The C# and Delphi ways escape the original name, but it's still the same name. In Delphi, you can even write the fully qualified name without the escape character.

            – Rob Kennedy
            Nov 8 '09 at 21:01





            But Ira, that changes the identifier. The C# and Delphi ways escape the original name, but it's still the same name. In Delphi, you can even write the fully qualified name without the escape character.

            – Rob Kennedy
            Nov 8 '09 at 21:01




            1




            1





            This becomes especially visible in Xml or JSON serialization as the serialized text would have ZZZ as part of the tag name <ZZZstring></ZZZstring>, whereas in C#, @string would serialize to <string></string>.

            – Rhyous
            Mar 31 '16 at 22:21





            This becomes especially visible in Xml or JSON serialization as the serialized text would have ZZZ as part of the tag name <ZZZstring></ZZZstring>, whereas in C#, @string would serialize to <string></string>.

            – Rhyous
            Mar 31 '16 at 22:21











            11














            Most often this issue comes up for "class", in this case it is customary to write "clazz".






            share|improve this answer



















            • 4





              Is there a best practices alternatives list for all keywords?

              – Ciro Santilli 新疆改造中心 六四事件 法轮功
              Apr 9 '15 at 9:23


















            11














            Most often this issue comes up for "class", in this case it is customary to write "clazz".






            share|improve this answer



















            • 4





              Is there a best practices alternatives list for all keywords?

              – Ciro Santilli 新疆改造中心 六四事件 法轮功
              Apr 9 '15 at 9:23
















            11












            11








            11







            Most often this issue comes up for "class", in this case it is customary to write "clazz".






            share|improve this answer













            Most often this issue comes up for "class", in this case it is customary to write "clazz".







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 8 '09 at 11:54









            starbluestarblue

            44.9k1176136




            44.9k1176136








            • 4





              Is there a best practices alternatives list for all keywords?

              – Ciro Santilli 新疆改造中心 六四事件 法轮功
              Apr 9 '15 at 9:23
















            • 4





              Is there a best practices alternatives list for all keywords?

              – Ciro Santilli 新疆改造中心 六四事件 法轮功
              Apr 9 '15 at 9:23










            4




            4





            Is there a best practices alternatives list for all keywords?

            – Ciro Santilli 新疆改造中心 六四事件 法轮功
            Apr 9 '15 at 9:23







            Is there a best practices alternatives list for all keywords?

            – Ciro Santilli 新疆改造中心 六四事件 法轮功
            Apr 9 '15 at 9:23













            6














            No, you can't do this. For more information please go to JLS Sections 3.8, 3.9




            The following character sequences,
            formed from ASCII letters, are
            reserved for use as keywords and
            cannot be used as identifiers (§3.8):



            Keyword: one of
            abstract continue for new switch
            assert default if package synchronized
            boolean do goto private this
            break double implements protected throw
            byte else import public throws
            case enum instanceof return transient
            catch extends int short try
            char final interface static void
            class finally long strictfp volatile
            const float native super while






            share|improve this answer



















            • 2





              shame about super

              – inspite
              Jan 8 '09 at 12:07
















            6














            No, you can't do this. For more information please go to JLS Sections 3.8, 3.9




            The following character sequences,
            formed from ASCII letters, are
            reserved for use as keywords and
            cannot be used as identifiers (§3.8):



            Keyword: one of
            abstract continue for new switch
            assert default if package synchronized
            boolean do goto private this
            break double implements protected throw
            byte else import public throws
            case enum instanceof return transient
            catch extends int short try
            char final interface static void
            class finally long strictfp volatile
            const float native super while






            share|improve this answer



















            • 2





              shame about super

              – inspite
              Jan 8 '09 at 12:07














            6












            6








            6







            No, you can't do this. For more information please go to JLS Sections 3.8, 3.9




            The following character sequences,
            formed from ASCII letters, are
            reserved for use as keywords and
            cannot be used as identifiers (§3.8):



            Keyword: one of
            abstract continue for new switch
            assert default if package synchronized
            boolean do goto private this
            break double implements protected throw
            byte else import public throws
            case enum instanceof return transient
            catch extends int short try
            char final interface static void
            class finally long strictfp volatile
            const float native super while






            share|improve this answer













            No, you can't do this. For more information please go to JLS Sections 3.8, 3.9




            The following character sequences,
            formed from ASCII letters, are
            reserved for use as keywords and
            cannot be used as identifiers (§3.8):



            Keyword: one of
            abstract continue for new switch
            assert default if package synchronized
            boolean do goto private this
            break double implements protected throw
            byte else import public throws
            case enum instanceof return transient
            catch extends int short try
            char final interface static void
            class finally long strictfp volatile
            const float native super while







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 8 '09 at 11:56









            bruno condebruno conde

            42.2k1088109




            42.2k1088109








            • 2





              shame about super

              – inspite
              Jan 8 '09 at 12:07














            • 2





              shame about super

              – inspite
              Jan 8 '09 at 12:07








            2




            2





            shame about super

            – inspite
            Jan 8 '09 at 12:07





            shame about super

            – inspite
            Jan 8 '09 at 12:07











            6














            Strictly speaking you can't, unless you get your hands on a buggy compiler implementation that doesn't adhere to the Java language spec.



            But where there's a will, there's a way. Copy the following code into your IDE, switch the source file encoding to UTF-16 and here we go:



            public class HelloWorld {

            public static void main(String args) {

            HelloWorld.nеw();
            }

            public static void nеw () {
            System.out.println("Hello,World");
            }

            }


            This code is a well-formed and valid Java class. However, as you have guessed there is a little trick: the 'е' character within "new" identifier does not belong to the ASCII character set, it is actually a cyrrilic 'е' (prounanced 'YE').



            Current Java language spec explicitly permits, and this an important point to make, the use of Unicode for naming identifiers. That means that one has an ability to freely call her or his classes in French, Chinise or Russian if they wish. It is also possible to mix and match the alphabets within code. And historically, some letters within Latin and other alphabets are lookalikes.



            As a result: no, you can't use the reserved words as identifiers, but you can use identifiers that look exactly like reserved words.



            Whether anyone should be doing it is a totally different matter.






            share|improve this answer





















            • 2





              Down voted because these kinds of tricks can lead to bugs and behavior that is not easily understood. Why not name it 'n3w' or anything else as it is intended to change the behavior.

              – CodeMonkeyKing
              Jul 13 '15 at 23:14






            • 1





              @CodeMonkeyKing, unfortunately not everyone and not in every scenario follows best naming conventions and it is entirely possible that someone might come across a piece of code that included an identifier looking exactly as a reserved word and, perplexingly, compiled without errors. This answer would come to the rescue then. Besides it is answering the actual question being asked. "Whether anyone should be doing it is a totally different matter."

              – Vlad Gudim
              Aug 4 '15 at 13:48






            • 3





              If I parse the OP's question exactly the answer you suggest only appears to the human eye to be a keyword and is not a "tricky way to use a Java reserved word". I would say from experience that these are the most difficult bugs to find. The real answer to this question is that reserved keywords are in fact reserved.

              – CodeMonkeyKing
              Aug 4 '15 at 18:04






            • 1





              I feel like this answer deserves a "Don't try this at home (or in the workplace)" label.

              – Chris Kerekes
              May 26 '17 at 17:42


















            6














            Strictly speaking you can't, unless you get your hands on a buggy compiler implementation that doesn't adhere to the Java language spec.



            But where there's a will, there's a way. Copy the following code into your IDE, switch the source file encoding to UTF-16 and here we go:



            public class HelloWorld {

            public static void main(String args) {

            HelloWorld.nеw();
            }

            public static void nеw () {
            System.out.println("Hello,World");
            }

            }


            This code is a well-formed and valid Java class. However, as you have guessed there is a little trick: the 'е' character within "new" identifier does not belong to the ASCII character set, it is actually a cyrrilic 'е' (prounanced 'YE').



            Current Java language spec explicitly permits, and this an important point to make, the use of Unicode for naming identifiers. That means that one has an ability to freely call her or his classes in French, Chinise or Russian if they wish. It is also possible to mix and match the alphabets within code. And historically, some letters within Latin and other alphabets are lookalikes.



            As a result: no, you can't use the reserved words as identifiers, but you can use identifiers that look exactly like reserved words.



            Whether anyone should be doing it is a totally different matter.






            share|improve this answer





















            • 2





              Down voted because these kinds of tricks can lead to bugs and behavior that is not easily understood. Why not name it 'n3w' or anything else as it is intended to change the behavior.

              – CodeMonkeyKing
              Jul 13 '15 at 23:14






            • 1





              @CodeMonkeyKing, unfortunately not everyone and not in every scenario follows best naming conventions and it is entirely possible that someone might come across a piece of code that included an identifier looking exactly as a reserved word and, perplexingly, compiled without errors. This answer would come to the rescue then. Besides it is answering the actual question being asked. "Whether anyone should be doing it is a totally different matter."

              – Vlad Gudim
              Aug 4 '15 at 13:48






            • 3





              If I parse the OP's question exactly the answer you suggest only appears to the human eye to be a keyword and is not a "tricky way to use a Java reserved word". I would say from experience that these are the most difficult bugs to find. The real answer to this question is that reserved keywords are in fact reserved.

              – CodeMonkeyKing
              Aug 4 '15 at 18:04






            • 1





              I feel like this answer deserves a "Don't try this at home (or in the workplace)" label.

              – Chris Kerekes
              May 26 '17 at 17:42
















            6












            6








            6







            Strictly speaking you can't, unless you get your hands on a buggy compiler implementation that doesn't adhere to the Java language spec.



            But where there's a will, there's a way. Copy the following code into your IDE, switch the source file encoding to UTF-16 and here we go:



            public class HelloWorld {

            public static void main(String args) {

            HelloWorld.nеw();
            }

            public static void nеw () {
            System.out.println("Hello,World");
            }

            }


            This code is a well-formed and valid Java class. However, as you have guessed there is a little trick: the 'е' character within "new" identifier does not belong to the ASCII character set, it is actually a cyrrilic 'е' (prounanced 'YE').



            Current Java language spec explicitly permits, and this an important point to make, the use of Unicode for naming identifiers. That means that one has an ability to freely call her or his classes in French, Chinise or Russian if they wish. It is also possible to mix and match the alphabets within code. And historically, some letters within Latin and other alphabets are lookalikes.



            As a result: no, you can't use the reserved words as identifiers, but you can use identifiers that look exactly like reserved words.



            Whether anyone should be doing it is a totally different matter.






            share|improve this answer















            Strictly speaking you can't, unless you get your hands on a buggy compiler implementation that doesn't adhere to the Java language spec.



            But where there's a will, there's a way. Copy the following code into your IDE, switch the source file encoding to UTF-16 and here we go:



            public class HelloWorld {

            public static void main(String args) {

            HelloWorld.nеw();
            }

            public static void nеw () {
            System.out.println("Hello,World");
            }

            }


            This code is a well-formed and valid Java class. However, as you have guessed there is a little trick: the 'е' character within "new" identifier does not belong to the ASCII character set, it is actually a cyrrilic 'е' (prounanced 'YE').



            Current Java language spec explicitly permits, and this an important point to make, the use of Unicode for naming identifiers. That means that one has an ability to freely call her or his classes in French, Chinise or Russian if they wish. It is also possible to mix and match the alphabets within code. And historically, some letters within Latin and other alphabets are lookalikes.



            As a result: no, you can't use the reserved words as identifiers, but you can use identifiers that look exactly like reserved words.



            Whether anyone should be doing it is a totally different matter.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 14 '09 at 20:24

























            answered Jan 14 '09 at 20:18









            Vlad GudimVlad Gudim

            16.7k146391




            16.7k146391








            • 2





              Down voted because these kinds of tricks can lead to bugs and behavior that is not easily understood. Why not name it 'n3w' or anything else as it is intended to change the behavior.

              – CodeMonkeyKing
              Jul 13 '15 at 23:14






            • 1





              @CodeMonkeyKing, unfortunately not everyone and not in every scenario follows best naming conventions and it is entirely possible that someone might come across a piece of code that included an identifier looking exactly as a reserved word and, perplexingly, compiled without errors. This answer would come to the rescue then. Besides it is answering the actual question being asked. "Whether anyone should be doing it is a totally different matter."

              – Vlad Gudim
              Aug 4 '15 at 13:48






            • 3





              If I parse the OP's question exactly the answer you suggest only appears to the human eye to be a keyword and is not a "tricky way to use a Java reserved word". I would say from experience that these are the most difficult bugs to find. The real answer to this question is that reserved keywords are in fact reserved.

              – CodeMonkeyKing
              Aug 4 '15 at 18:04






            • 1





              I feel like this answer deserves a "Don't try this at home (or in the workplace)" label.

              – Chris Kerekes
              May 26 '17 at 17:42
















            • 2





              Down voted because these kinds of tricks can lead to bugs and behavior that is not easily understood. Why not name it 'n3w' or anything else as it is intended to change the behavior.

              – CodeMonkeyKing
              Jul 13 '15 at 23:14






            • 1





              @CodeMonkeyKing, unfortunately not everyone and not in every scenario follows best naming conventions and it is entirely possible that someone might come across a piece of code that included an identifier looking exactly as a reserved word and, perplexingly, compiled without errors. This answer would come to the rescue then. Besides it is answering the actual question being asked. "Whether anyone should be doing it is a totally different matter."

              – Vlad Gudim
              Aug 4 '15 at 13:48






            • 3





              If I parse the OP's question exactly the answer you suggest only appears to the human eye to be a keyword and is not a "tricky way to use a Java reserved word". I would say from experience that these are the most difficult bugs to find. The real answer to this question is that reserved keywords are in fact reserved.

              – CodeMonkeyKing
              Aug 4 '15 at 18:04






            • 1





              I feel like this answer deserves a "Don't try this at home (or in the workplace)" label.

              – Chris Kerekes
              May 26 '17 at 17:42










            2




            2





            Down voted because these kinds of tricks can lead to bugs and behavior that is not easily understood. Why not name it 'n3w' or anything else as it is intended to change the behavior.

            – CodeMonkeyKing
            Jul 13 '15 at 23:14





            Down voted because these kinds of tricks can lead to bugs and behavior that is not easily understood. Why not name it 'n3w' or anything else as it is intended to change the behavior.

            – CodeMonkeyKing
            Jul 13 '15 at 23:14




            1




            1





            @CodeMonkeyKing, unfortunately not everyone and not in every scenario follows best naming conventions and it is entirely possible that someone might come across a piece of code that included an identifier looking exactly as a reserved word and, perplexingly, compiled without errors. This answer would come to the rescue then. Besides it is answering the actual question being asked. "Whether anyone should be doing it is a totally different matter."

            – Vlad Gudim
            Aug 4 '15 at 13:48





            @CodeMonkeyKing, unfortunately not everyone and not in every scenario follows best naming conventions and it is entirely possible that someone might come across a piece of code that included an identifier looking exactly as a reserved word and, perplexingly, compiled without errors. This answer would come to the rescue then. Besides it is answering the actual question being asked. "Whether anyone should be doing it is a totally different matter."

            – Vlad Gudim
            Aug 4 '15 at 13:48




            3




            3





            If I parse the OP's question exactly the answer you suggest only appears to the human eye to be a keyword and is not a "tricky way to use a Java reserved word". I would say from experience that these are the most difficult bugs to find. The real answer to this question is that reserved keywords are in fact reserved.

            – CodeMonkeyKing
            Aug 4 '15 at 18:04





            If I parse the OP's question exactly the answer you suggest only appears to the human eye to be a keyword and is not a "tricky way to use a Java reserved word". I would say from experience that these are the most difficult bugs to find. The real answer to this question is that reserved keywords are in fact reserved.

            – CodeMonkeyKing
            Aug 4 '15 at 18:04




            1




            1





            I feel like this answer deserves a "Don't try this at home (or in the workplace)" label.

            – Chris Kerekes
            May 26 '17 at 17:42







            I feel like this answer deserves a "Don't try this at home (or in the workplace)" label.

            – Chris Kerekes
            May 26 '17 at 17:42













            4














            Yes, there is.
            You have to use reserved words from the future.
            Like what happened with different methods called assert() in pre-1.4 code.



            Hope it helps!






            share|improve this answer




























              4














              Yes, there is.
              You have to use reserved words from the future.
              Like what happened with different methods called assert() in pre-1.4 code.



              Hope it helps!






              share|improve this answer


























                4












                4








                4







                Yes, there is.
                You have to use reserved words from the future.
                Like what happened with different methods called assert() in pre-1.4 code.



                Hope it helps!






                share|improve this answer













                Yes, there is.
                You have to use reserved words from the future.
                Like what happened with different methods called assert() in pre-1.4 code.



                Hope it helps!







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 8 '09 at 11:59









                Yoni RoitYoni Roit

                21.9k52930




                21.9k52930























                    3














                    Huh? Why would you want to do that? You can write them in l33t, that will fool the compiler.



                    class cl4ss {
                    String r3turn() {
                    return "but why?";
                    }
                    }





                    share|improve this answer



















                    • 3





                      Technically - you never fool the compiler as the above example is completely legal Java.

                      – CodeMonkeyKing
                      Aug 4 '15 at 18:11
















                    3














                    Huh? Why would you want to do that? You can write them in l33t, that will fool the compiler.



                    class cl4ss {
                    String r3turn() {
                    return "but why?";
                    }
                    }





                    share|improve this answer



















                    • 3





                      Technically - you never fool the compiler as the above example is completely legal Java.

                      – CodeMonkeyKing
                      Aug 4 '15 at 18:11














                    3












                    3








                    3







                    Huh? Why would you want to do that? You can write them in l33t, that will fool the compiler.



                    class cl4ss {
                    String r3turn() {
                    return "but why?";
                    }
                    }





                    share|improve this answer













                    Huh? Why would you want to do that? You can write them in l33t, that will fool the compiler.



                    class cl4ss {
                    String r3turn() {
                    return "but why?";
                    }
                    }






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 8 '09 at 11:49









                    Miserable VariableMiserable Variable

                    23.4k1155107




                    23.4k1155107








                    • 3





                      Technically - you never fool the compiler as the above example is completely legal Java.

                      – CodeMonkeyKing
                      Aug 4 '15 at 18:11














                    • 3





                      Technically - you never fool the compiler as the above example is completely legal Java.

                      – CodeMonkeyKing
                      Aug 4 '15 at 18:11








                    3




                    3





                    Technically - you never fool the compiler as the above example is completely legal Java.

                    – CodeMonkeyKing
                    Aug 4 '15 at 18:11





                    Technically - you never fool the compiler as the above example is completely legal Java.

                    – CodeMonkeyKing
                    Aug 4 '15 at 18:11











                    3














                    I know it's old question still, might help someone.



                    It's possible by using GSON's Field Naming Support



                    eg.



                    @SerializedName("new")
                    private String New;
                    public String getNew ()
                    {
                    return New;
                    }
                    public void setNew (String aNew)
                    {
                    New = aNew;
                    }





                    share|improve this answer





















                    • 1





                      It helped me, thanks

                      – Jake Gaston
                      Mar 2 '18 at 16:21











                    • Glad to be of help @jake gaston It's workaround i needed at one Project which already had defined variables which couldn't be changed.

                      – SK16
                      Mar 3 '18 at 6:39


















                    3














                    I know it's old question still, might help someone.



                    It's possible by using GSON's Field Naming Support



                    eg.



                    @SerializedName("new")
                    private String New;
                    public String getNew ()
                    {
                    return New;
                    }
                    public void setNew (String aNew)
                    {
                    New = aNew;
                    }





                    share|improve this answer





















                    • 1





                      It helped me, thanks

                      – Jake Gaston
                      Mar 2 '18 at 16:21











                    • Glad to be of help @jake gaston It's workaround i needed at one Project which already had defined variables which couldn't be changed.

                      – SK16
                      Mar 3 '18 at 6:39
















                    3












                    3








                    3







                    I know it's old question still, might help someone.



                    It's possible by using GSON's Field Naming Support



                    eg.



                    @SerializedName("new")
                    private String New;
                    public String getNew ()
                    {
                    return New;
                    }
                    public void setNew (String aNew)
                    {
                    New = aNew;
                    }





                    share|improve this answer















                    I know it's old question still, might help someone.



                    It's possible by using GSON's Field Naming Support



                    eg.



                    @SerializedName("new")
                    private String New;
                    public String getNew ()
                    {
                    return New;
                    }
                    public void setNew (String aNew)
                    {
                    New = aNew;
                    }






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited May 11 '17 at 17:06









                    Remy Lebeau

                    336k18259453




                    336k18259453










                    answered Nov 25 '16 at 13:18









                    SK16SK16

                    428513




                    428513








                    • 1





                      It helped me, thanks

                      – Jake Gaston
                      Mar 2 '18 at 16:21











                    • Glad to be of help @jake gaston It's workaround i needed at one Project which already had defined variables which couldn't be changed.

                      – SK16
                      Mar 3 '18 at 6:39
















                    • 1





                      It helped me, thanks

                      – Jake Gaston
                      Mar 2 '18 at 16:21











                    • Glad to be of help @jake gaston It's workaround i needed at one Project which already had defined variables which couldn't be changed.

                      – SK16
                      Mar 3 '18 at 6:39










                    1




                    1





                    It helped me, thanks

                    – Jake Gaston
                    Mar 2 '18 at 16:21





                    It helped me, thanks

                    – Jake Gaston
                    Mar 2 '18 at 16:21













                    Glad to be of help @jake gaston It's workaround i needed at one Project which already had defined variables which couldn't be changed.

                    – SK16
                    Mar 3 '18 at 6:39







                    Glad to be of help @jake gaston It's workaround i needed at one Project which already had defined variables which couldn't be changed.

                    – SK16
                    Mar 3 '18 at 6:39













                    2














                    It's bad enough some case-sensitive languages allow things like the following:



                    class New;

                    class Something
                    {
                    New makeNew()
                    {
                    return new New();
                    }
                    }


                    But why would you ever want to be able to write a line of code like this:



                    class new;

                    class Something
                    {
                    bool if;

                    new makeNew()
                    {
                    return if ? new new() : null;
                    }
                    }


                    Just take a look at the syntax highlighting. Even it gets confused!






                    share|improve this answer



















                    • 4





                      You can ridicule any feature by understanding its potential applications wrong or by giving bad examples. That doesn't imply that better uses don't exist. Sometimes, a reserved word in indeed the most natural name (as is often the case when the identifier clazz oder klass is used).

                      – Konrad Rudolph
                      Jan 14 '09 at 20:31











                    • I don't think what I supposed was a 'bad example'. A bit over the top, yes, but not bad. Maybe the most natural name is a reserved word, is it good to use that name? Reserved words are reserved for a reason. It's easy to distinguish 'class' and 'clazz'. Can you distinguish 'class' and 'class'?

                      – lc.
                      Jan 15 '09 at 7:00






                    • 3





                      But not all languages agree what the reserved words are. In Scala, it is perfectly natural, to name a method that returns the default value default. This works perfectly fine, because default is not a reserved word in Scala. It is, however, a reserved word in Java, thus, you cannot call this method from Java, even though you have perfectly legitimate reason to do so.

                      – Jörg W Mittag
                      Aug 25 '15 at 22:11
















                    2














                    It's bad enough some case-sensitive languages allow things like the following:



                    class New;

                    class Something
                    {
                    New makeNew()
                    {
                    return new New();
                    }
                    }


                    But why would you ever want to be able to write a line of code like this:



                    class new;

                    class Something
                    {
                    bool if;

                    new makeNew()
                    {
                    return if ? new new() : null;
                    }
                    }


                    Just take a look at the syntax highlighting. Even it gets confused!






                    share|improve this answer



















                    • 4





                      You can ridicule any feature by understanding its potential applications wrong or by giving bad examples. That doesn't imply that better uses don't exist. Sometimes, a reserved word in indeed the most natural name (as is often the case when the identifier clazz oder klass is used).

                      – Konrad Rudolph
                      Jan 14 '09 at 20:31











                    • I don't think what I supposed was a 'bad example'. A bit over the top, yes, but not bad. Maybe the most natural name is a reserved word, is it good to use that name? Reserved words are reserved for a reason. It's easy to distinguish 'class' and 'clazz'. Can you distinguish 'class' and 'class'?

                      – lc.
                      Jan 15 '09 at 7:00






                    • 3





                      But not all languages agree what the reserved words are. In Scala, it is perfectly natural, to name a method that returns the default value default. This works perfectly fine, because default is not a reserved word in Scala. It is, however, a reserved word in Java, thus, you cannot call this method from Java, even though you have perfectly legitimate reason to do so.

                      – Jörg W Mittag
                      Aug 25 '15 at 22:11














                    2












                    2








                    2







                    It's bad enough some case-sensitive languages allow things like the following:



                    class New;

                    class Something
                    {
                    New makeNew()
                    {
                    return new New();
                    }
                    }


                    But why would you ever want to be able to write a line of code like this:



                    class new;

                    class Something
                    {
                    bool if;

                    new makeNew()
                    {
                    return if ? new new() : null;
                    }
                    }


                    Just take a look at the syntax highlighting. Even it gets confused!






                    share|improve this answer













                    It's bad enough some case-sensitive languages allow things like the following:



                    class New;

                    class Something
                    {
                    New makeNew()
                    {
                    return new New();
                    }
                    }


                    But why would you ever want to be able to write a line of code like this:



                    class new;

                    class Something
                    {
                    bool if;

                    new makeNew()
                    {
                    return if ? new new() : null;
                    }
                    }


                    Just take a look at the syntax highlighting. Even it gets confused!







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 8 '09 at 12:00









                    lc.lc.

                    89k19125155




                    89k19125155








                    • 4





                      You can ridicule any feature by understanding its potential applications wrong or by giving bad examples. That doesn't imply that better uses don't exist. Sometimes, a reserved word in indeed the most natural name (as is often the case when the identifier clazz oder klass is used).

                      – Konrad Rudolph
                      Jan 14 '09 at 20:31











                    • I don't think what I supposed was a 'bad example'. A bit over the top, yes, but not bad. Maybe the most natural name is a reserved word, is it good to use that name? Reserved words are reserved for a reason. It's easy to distinguish 'class' and 'clazz'. Can you distinguish 'class' and 'class'?

                      – lc.
                      Jan 15 '09 at 7:00






                    • 3





                      But not all languages agree what the reserved words are. In Scala, it is perfectly natural, to name a method that returns the default value default. This works perfectly fine, because default is not a reserved word in Scala. It is, however, a reserved word in Java, thus, you cannot call this method from Java, even though you have perfectly legitimate reason to do so.

                      – Jörg W Mittag
                      Aug 25 '15 at 22:11














                    • 4





                      You can ridicule any feature by understanding its potential applications wrong or by giving bad examples. That doesn't imply that better uses don't exist. Sometimes, a reserved word in indeed the most natural name (as is often the case when the identifier clazz oder klass is used).

                      – Konrad Rudolph
                      Jan 14 '09 at 20:31











                    • I don't think what I supposed was a 'bad example'. A bit over the top, yes, but not bad. Maybe the most natural name is a reserved word, is it good to use that name? Reserved words are reserved for a reason. It's easy to distinguish 'class' and 'clazz'. Can you distinguish 'class' and 'class'?

                      – lc.
                      Jan 15 '09 at 7:00






                    • 3





                      But not all languages agree what the reserved words are. In Scala, it is perfectly natural, to name a method that returns the default value default. This works perfectly fine, because default is not a reserved word in Scala. It is, however, a reserved word in Java, thus, you cannot call this method from Java, even though you have perfectly legitimate reason to do so.

                      – Jörg W Mittag
                      Aug 25 '15 at 22:11








                    4




                    4





                    You can ridicule any feature by understanding its potential applications wrong or by giving bad examples. That doesn't imply that better uses don't exist. Sometimes, a reserved word in indeed the most natural name (as is often the case when the identifier clazz oder klass is used).

                    – Konrad Rudolph
                    Jan 14 '09 at 20:31





                    You can ridicule any feature by understanding its potential applications wrong or by giving bad examples. That doesn't imply that better uses don't exist. Sometimes, a reserved word in indeed the most natural name (as is often the case when the identifier clazz oder klass is used).

                    – Konrad Rudolph
                    Jan 14 '09 at 20:31













                    I don't think what I supposed was a 'bad example'. A bit over the top, yes, but not bad. Maybe the most natural name is a reserved word, is it good to use that name? Reserved words are reserved for a reason. It's easy to distinguish 'class' and 'clazz'. Can you distinguish 'class' and 'class'?

                    – lc.
                    Jan 15 '09 at 7:00





                    I don't think what I supposed was a 'bad example'. A bit over the top, yes, but not bad. Maybe the most natural name is a reserved word, is it good to use that name? Reserved words are reserved for a reason. It's easy to distinguish 'class' and 'clazz'. Can you distinguish 'class' and 'class'?

                    – lc.
                    Jan 15 '09 at 7:00




                    3




                    3





                    But not all languages agree what the reserved words are. In Scala, it is perfectly natural, to name a method that returns the default value default. This works perfectly fine, because default is not a reserved word in Scala. It is, however, a reserved word in Java, thus, you cannot call this method from Java, even though you have perfectly legitimate reason to do so.

                    – Jörg W Mittag
                    Aug 25 '15 at 22:11





                    But not all languages agree what the reserved words are. In Scala, it is perfectly natural, to name a method that returns the default value default. This works perfectly fine, because default is not a reserved word in Scala. It is, however, a reserved word in Java, thus, you cannot call this method from Java, even though you have perfectly legitimate reason to do so.

                    – Jörg W Mittag
                    Aug 25 '15 at 22:11











                    2














                    There is no way to use reserved words with the javac compiler.



                    Technically, you can edit the names inside the class file once it's compiled to be anything you want: at that stage, the VM doesn't care, because it's not dealing with source code any more. I believe some obfuscators use this technique.






                    share|improve this answer




























                      2














                      There is no way to use reserved words with the javac compiler.



                      Technically, you can edit the names inside the class file once it's compiled to be anything you want: at that stage, the VM doesn't care, because it's not dealing with source code any more. I believe some obfuscators use this technique.






                      share|improve this answer


























                        2












                        2








                        2







                        There is no way to use reserved words with the javac compiler.



                        Technically, you can edit the names inside the class file once it's compiled to be anything you want: at that stage, the VM doesn't care, because it's not dealing with source code any more. I believe some obfuscators use this technique.






                        share|improve this answer













                        There is no way to use reserved words with the javac compiler.



                        Technically, you can edit the names inside the class file once it's compiled to be anything you want: at that stage, the VM doesn't care, because it's not dealing with source code any more. I believe some obfuscators use this technique.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jan 8 '09 at 16:37









                        Neil CoffeyNeil Coffey

                        18.6k65476




                        18.6k65476























                            2














                            PL/1 (a 1960's IBM mainframe programming language still around today) rather famously required that while some words act like keywords in certain contexts, all words can be used as identifiers. This isn't even that hard to do in a parser if you set out to be consistent about it. PL/1 was considered to a rather big langauge, and the langauge committee worried that many programmers wouldn't learn all of it, and then would get suprised when they tried to use the keyword from a part they didn't know as an identifier.
                            So you could write things like:



                            IF BEGIN=ELSE THEN CALL=3 ELSE CALL FOO(ENDIF) ENDIF


                            As others have noted here, the ability to do this isn't a recommendation.



                            The Java designers decided the number of keywords in the langauge was modest, and reserved the set. They even reserved 'GOTO', which isn't actually allowed in any real Java program.






                            share|improve this answer




























                              2














                              PL/1 (a 1960's IBM mainframe programming language still around today) rather famously required that while some words act like keywords in certain contexts, all words can be used as identifiers. This isn't even that hard to do in a parser if you set out to be consistent about it. PL/1 was considered to a rather big langauge, and the langauge committee worried that many programmers wouldn't learn all of it, and then would get suprised when they tried to use the keyword from a part they didn't know as an identifier.
                              So you could write things like:



                              IF BEGIN=ELSE THEN CALL=3 ELSE CALL FOO(ENDIF) ENDIF


                              As others have noted here, the ability to do this isn't a recommendation.



                              The Java designers decided the number of keywords in the langauge was modest, and reserved the set. They even reserved 'GOTO', which isn't actually allowed in any real Java program.






                              share|improve this answer


























                                2












                                2








                                2







                                PL/1 (a 1960's IBM mainframe programming language still around today) rather famously required that while some words act like keywords in certain contexts, all words can be used as identifiers. This isn't even that hard to do in a parser if you set out to be consistent about it. PL/1 was considered to a rather big langauge, and the langauge committee worried that many programmers wouldn't learn all of it, and then would get suprised when they tried to use the keyword from a part they didn't know as an identifier.
                                So you could write things like:



                                IF BEGIN=ELSE THEN CALL=3 ELSE CALL FOO(ENDIF) ENDIF


                                As others have noted here, the ability to do this isn't a recommendation.



                                The Java designers decided the number of keywords in the langauge was modest, and reserved the set. They even reserved 'GOTO', which isn't actually allowed in any real Java program.






                                share|improve this answer













                                PL/1 (a 1960's IBM mainframe programming language still around today) rather famously required that while some words act like keywords in certain contexts, all words can be used as identifiers. This isn't even that hard to do in a parser if you set out to be consistent about it. PL/1 was considered to a rather big langauge, and the langauge committee worried that many programmers wouldn't learn all of it, and then would get suprised when they tried to use the keyword from a part they didn't know as an identifier.
                                So you could write things like:



                                IF BEGIN=ELSE THEN CALL=3 ELSE CALL FOO(ENDIF) ENDIF


                                As others have noted here, the ability to do this isn't a recommendation.



                                The Java designers decided the number of keywords in the langauge was modest, and reserved the set. They even reserved 'GOTO', which isn't actually allowed in any real Java program.







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Nov 8 '09 at 21:02









                                Ira BaxterIra Baxter

                                80.4k10131266




                                80.4k10131266























                                    2














                                    Not sure what you're trying to do, but $ is a valid character in identifiers, so you could do, say:



                                    int $return = 5;



                                    It looks a little weird, but it does work.






                                    share|improve this answer





















                                    • 1





                                      This is similar to @identifier in C#, although it doesn't end in the same result (in C# the '@' does not become part of the identifier)

                                      – Hosam Aly
                                      Jan 24 '09 at 8:38
















                                    2














                                    Not sure what you're trying to do, but $ is a valid character in identifiers, so you could do, say:



                                    int $return = 5;



                                    It looks a little weird, but it does work.






                                    share|improve this answer





















                                    • 1





                                      This is similar to @identifier in C#, although it doesn't end in the same result (in C# the '@' does not become part of the identifier)

                                      – Hosam Aly
                                      Jan 24 '09 at 8:38














                                    2












                                    2








                                    2







                                    Not sure what you're trying to do, but $ is a valid character in identifiers, so you could do, say:



                                    int $return = 5;



                                    It looks a little weird, but it does work.






                                    share|improve this answer















                                    Not sure what you're trying to do, but $ is a valid character in identifiers, so you could do, say:



                                    int $return = 5;



                                    It looks a little weird, but it does work.







                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Oct 2 '11 at 9:34









                                    Bohemian

                                    297k65423557




                                    297k65423557










                                    answered Jan 9 '09 at 0:03









                                    Kris PrudenKris Pruden

                                    1,90342025




                                    1,90342025








                                    • 1





                                      This is similar to @identifier in C#, although it doesn't end in the same result (in C# the '@' does not become part of the identifier)

                                      – Hosam Aly
                                      Jan 24 '09 at 8:38














                                    • 1





                                      This is similar to @identifier in C#, although it doesn't end in the same result (in C# the '@' does not become part of the identifier)

                                      – Hosam Aly
                                      Jan 24 '09 at 8:38








                                    1




                                    1





                                    This is similar to @identifier in C#, although it doesn't end in the same result (in C# the '@' does not become part of the identifier)

                                    – Hosam Aly
                                    Jan 24 '09 at 8:38





                                    This is similar to @identifier in C#, although it doesn't end in the same result (in C# the '@' does not become part of the identifier)

                                    – Hosam Aly
                                    Jan 24 '09 at 8:38











                                    1














                                    In Scala you can use backticks. For example: myVarialbe.`class`






                                    share|improve this answer




























                                      1














                                      In Scala you can use backticks. For example: myVarialbe.`class`






                                      share|improve this answer


























                                        1












                                        1








                                        1







                                        In Scala you can use backticks. For example: myVarialbe.`class`






                                        share|improve this answer













                                        In Scala you can use backticks. For example: myVarialbe.`class`







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Sep 12 '12 at 17:21









                                        JackJack

                                        9,4391077146




                                        9,4391077146























                                            0














                                            If you really need to use a field/local variable/method named the same as a reserved word, I suggest appending an underscore at the end of the name:



                                            // JPA entity mapping class:

                                            private Boolean void_;

                                            public Boolean getVoid_() { ... }
                                            void setVoid_(Boolean void_) { ... }


                                            It is a more readable choice (IMHO) than appending chars at the beginning of the name (fVoid, aVoid, vVoid, etc.)



                                            The code above is a real world case that happened to me, working with a legacy database, in which the invoice table had a field named void indicating whether the document had been voided or not.






                                            share|improve this answer






























                                              0














                                              If you really need to use a field/local variable/method named the same as a reserved word, I suggest appending an underscore at the end of the name:



                                              // JPA entity mapping class:

                                              private Boolean void_;

                                              public Boolean getVoid_() { ... }
                                              void setVoid_(Boolean void_) { ... }


                                              It is a more readable choice (IMHO) than appending chars at the beginning of the name (fVoid, aVoid, vVoid, etc.)



                                              The code above is a real world case that happened to me, working with a legacy database, in which the invoice table had a field named void indicating whether the document had been voided or not.






                                              share|improve this answer




























                                                0












                                                0








                                                0







                                                If you really need to use a field/local variable/method named the same as a reserved word, I suggest appending an underscore at the end of the name:



                                                // JPA entity mapping class:

                                                private Boolean void_;

                                                public Boolean getVoid_() { ... }
                                                void setVoid_(Boolean void_) { ... }


                                                It is a more readable choice (IMHO) than appending chars at the beginning of the name (fVoid, aVoid, vVoid, etc.)



                                                The code above is a real world case that happened to me, working with a legacy database, in which the invoice table had a field named void indicating whether the document had been voided or not.






                                                share|improve this answer















                                                If you really need to use a field/local variable/method named the same as a reserved word, I suggest appending an underscore at the end of the name:



                                                // JPA entity mapping class:

                                                private Boolean void_;

                                                public Boolean getVoid_() { ... }
                                                void setVoid_(Boolean void_) { ... }


                                                It is a more readable choice (IMHO) than appending chars at the beginning of the name (fVoid, aVoid, vVoid, etc.)



                                                The code above is a real world case that happened to me, working with a legacy database, in which the invoice table had a field named void indicating whether the document had been voided or not.







                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Nov 28 '18 at 21:51

























                                                answered Nov 26 '18 at 15:49









                                                jpangamarcajpangamarca

                                                465927




                                                465927






























                                                    draft saved

                                                    draft discarded




















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid



                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.


                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function () {
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f423994%2freserved-words-as-variable-or-method-names%23new-answer', 'question_page');
                                                    }
                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown







                                                    Popular posts from this blog

                                                    A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

                                                    Calculate evaluation metrics using cross_val_predict sklearn

                                                    Insert data from modal to MySQL (multiple modal on website)