15

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?

3
  • Is there a reason why you'd want to use characters like these at the start of a variable, method, or anything? Commented Apr 18, 2012 at 14:10
  • 1
    I'm working on an extern domain specific language with Xtext that is executed on Java virtual machine. That's why I would like to make this precise :) Thanks! Commented Apr 18, 2012 at 14:15
  • 1
    possible duplicate of Legal identifiers in Java because of docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4 Commented Apr 4, 2015 at 8:19

3 Answers 3

15

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, and this is a nice view of it here, too. cui.unige.ch/isi/bnf/JAVA/identifier.html
1

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.

1 Comment

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
0

From the Java Tutorial:

"Although a method name can be any legal identifier, code conventions restrict method names." http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.