Friday, February 25, 2011

Using keywords as variable or function names

When you are writing a program, you find one of your variables need to be named as volatile because of its nature or some other reason.But you cannot name it as volatile because volatile is a keyword.So what to do?

prefix @ to the variable or the method name and it will allow you to compile.

public int test(int @volatile)
return @volatile++;
public int @volatile(int index)
return index++;

Another way is to prefix ‘_’. But if the ‘_’ is reserved for the private class field prefix as per your coding guidelines you cannot use it.

There is one question exists still.If I prefix @ , its no more the the same volatile word.So why can’t I change the variable name to something else such as volatileIndex without the @ char? Then I don’t have any answer.Change the name as you like :-)

No comments: