answersLogoWhite

0


Best Answer

Some basic conventions are:
1. you cannot have keywords as names
2. All identifiers have to start with an alphabet
3. Except '_' and '$' no other special character is allowed in names
4. When there are multiple words in an identifier we usually capitalize the first alphabets of each word. For example if an identifier represents name of employee then we name it as nameOfEmployee. The first word would remain normal with no capitalization

etc...

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

All variable names must begin with a letter of the alphabet, an underscore ( _ ), or a dollar sign ($). The rest of the characters may be any of those previously mentioned plus the digits 0-9.

The convention is to always use a letter of the alphabet. The dollar sign and the underscore are discouraged.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

You can name your variable anything you like, using most symbols. A few symbols are excluded, like a hyphen (which would be confused with a minus), and the first symbol for the variable name can't be a digit. However, try not to use a lot of weird symbols; just using letters, digits, and the underscore is probably safest.

There are also certain recommended conventions for naming variables. The compiler won't complain if you don't follow them, but they certainly help to make your program more readable. Mainly:

  • Write the variable name in lowercase. Examples: age, person.
  • If the variable name contains several words, start every word, except the first, with uppercase. Examples: dateOfBirth, anualSalary.
  • Give your variables meaningful names. In the ideal case, just looking at the variable name should tell a programmer what it is about, without reading lots of comments in the program.
  • Constants are written all in uppercase. To separate words, use the underscore (low hyphen). Example: DAYS_PER_WEEK (which would be defined as 7).
  • Names for methods follow the same conventions as variables.
  • Names for classes start with an uppercase letter. Example: String, Calendar. Once again, if the name contains several words, start each word with an uppercase letter. Example: ComplexNumber.

Search for the variable naming conventions from Sun, for more details on the recommended naming conventions.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What are the rules for naming variables in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What are the differences between C and Java reference variables?

Java does not have the concept of Reference Variables. We cannot access the memory location where the data is stored in Java.


What does the acronym JNDI stand for?

The acronym 'JNDI' stands for Java Naming and Directory Interface. What this refers to is the naming system for Java, a computer programming language.


Does java consider the variables Hello and hello to be different variables?

no


Why there are no global variables in java?

Global variables are globally accessible. Java does not support globally accessible variables due to following reasons:The global variables breaks the referential transparencyGlobal variables creates collisions in namespac


What is the syntax of global variable in java?

There's no global variables in Java.


How we can get address of variables in Java as pointer variables in C?

At any given point of time you cann't get the address of a variables of java program. This is meant for security purpose only.


In java all variables declared using a class are?

Reference variables


Are static variables serialized in java?

No, static variables are not serialized.


What are the rules for naming new elements?

The IUPAC rules for naming new chemical elements are at this link.


Can you define variables in interface in java?

yes we can define a variable in an interface in java.


Are variables automatically initialized by Java if the programmer does not?

no


Can a java program has two different variable?

Yes. You can have as many variables as you want in Java