Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Java:Tutorial - The Variable

case sensitive myvariable

  • Please log in to reply
No replies to this topic

#1 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 09 December 2006 - 07:59 AM

The variable is probably the most important feature in any programming language. It gives the programmer the ability to store a value in the computers memory and call upon it when ever they want. We will come across variable declarations in a variety of situations in Java. At its most basic, a variable declaration consists of a type (discussed in another tutorial) and an identifier. They may or may not have an access control modifier. A variable may look similar to this

private int x = 0;
String colorBlue = “blue”;

There are also different kinds of variables:
Instance Variables: Instance variables are so named because they are variables that are associated with instances of a class; each instance of a class has its own set of instance variables.

Class Variables: Class variables are static, no mater how many instances of the class there are, there is only instance of a class variable.

Local Variables: Local variables have no meaning outside their method. I often refer to them as “trash variables” because after they are used in a method, they are discarded as “trash.”


Naming Conventions
1. A variable can consist of any alphanumeric character and must begin with a letter, a dollar sign, or an underscore. The use of a dollars sign is highly discouraged and so is the underscore. However, it is becoming a popular trend to define instance variables with an underscore.
2. Variables are case sensitive. MyVariable is NOT the same as myvariable.
3. It is convention to start your variable names with a letter, and each subsequent word capitalize. It is also a bad programming practice to use abbreviations as variables and ambiguous variables.

Poor program practices would be
int [B]MyVar [/B]= 0; //var???
int [B]x[/B] = 0; //what is x?

Perfred programming practices
int [B]myVariable [/B]= 0;
int [B]xLocation [/B]= 0;

  • -1





Also tagged with one or more of these keywords: case sensitive, myvariable

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download