What are the valid characters for a Java method name?

21,868

Solution 1

Yes, method names and variable names are what's called "identifiers". Identifiers all share the same rules regarding accepted characters. Take a look at §3.8 from the Java Language Specification to find out exactly what an identifier may contain, and §6.2 for an explanation about how identifiers are used.

Solution 2

You might be surprised when having unusual characters for method, such as:

public void mój_brzuch_zacznie_burczeć()

and it works pretty nice. Take a look at this blog to see more fancy examples.

Share:
21,868
kon
Author by

kon

Updated on July 09, 2022

Comments

  • kon
    kon almost 2 years

    I read about the naming of Java variables. It says that Java variables cannot start with any numbers and special characters except for $ and _.

    Some valid examples:

    int count;
    int _count;
    int $count;
    

    And some invalid examples:

    int %count;
    int 4count;
    int #count;
    

    Do the same rules apply to method names?

  • Todd Murray
    Todd Murray about 12 years
    Yes, and this is a nice view of it here, too. cui.unige.ch/isi/bnf/JAVA/identifier.html
  • Joter
    Joter about 3 years
    or U+02BC (Modifier Letter Apostrophe) void itʼs_a_method(). You can use any modifier letters unicode-table.com/en/blocks/spacing-modifier-letters