This is a discussion on Java:Tutorial - The Variable within the Java Programming forums, part of the Software Development category; The variable is probably the most important feature in any programming language. It gives the programmer the ability to store ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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 Code: Code: private int x = 0; String colorBlue = “blue”; 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 Code: Code: int MyVar = 0; //var??? int x = 0; //what is x? Code: Code: int myVariable = 0; int xLocation = 0; |
| Sponsored Links |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Java:Tutorial - A better looking GUI | pranky | Java Programming | 2 | 03-08-2007 12:34 PM |
| Java:Tutorial - Arrays | pranky | Java Programming | 0 | 02-24-2007 12:54 AM |
| Java:Tutorial - Flow Control | pranky | Java Programming | 0 | 02-24-2007 12:53 AM |
| Java:Tutorial - Getting Started | pranky | Java Programming | 0 | 02-24-2007 12:48 AM |
| Java:Tutorial - Tic-Tac-Toe | pranky | Java Programming | 6 | 02-23-2007 09:48 AM |