Java: Variable Naming

Variables are pretty self explanatory, they are used to hold references to points in memory that store information (ok, maybe not that self explanatory). Things like strings, integers, doubles, objects, everything, in fact, can be stored as a variable. They are for the most part pretty simple, but, as always, there’s some odd and confusing edge cases to be aware of when using them.

Variable Names

Similar to valid class names, there are rules to what a variable can be named. Keywords like class, int, string etc. can’t be used as a variable name, for the obvious reason that the compiler isn’t really going to know what you’re talking about if you write:

int int = 5;

I could just list out the rest of the rules for variable declaration, but what would be the fun in that? Take a look at the following and see if you can tell how many of them are valid variable declarations.

public class NumbersVariablesDec {

int my_Number = 4;
int _OtherNumber = 5;
int $ = 1;
int __$____$_$_$ = 1;
int Class = 1;
int class = 1;

//Value assignment here, the names are valid
int numberOne = 017;
int numberTwo = 0xFF;
int numberThree = 0b10;
int numberFour = 1_0_0;
int numberFive = _100;
String one = 1;
String myNumber = "4";
}

The code above is split into two parts, the first half is focused on what makes an appropriate variable name, the second half focuses on what is valid to store in a variable once you’ve defined its type. For the first part, only the variable name class (line 8) is invalid, all the other variable names are valid in Java syntax. Yes, you’d lose friends at your job very quickly if you start naming your variables _$_, but it’s still valid code. Variable names cannot be Java keywords, can’t start with a number, and that’s really it.

What about the second half? Only two of them are invalid: lines 15 and 16. Java numbers can have underscores in their declaration, it’s to allow you to make large numbers more readable by using underscores in place of commas, but the underscores must be between two digits, which means that they can’t be at the start or end of the number, or either side of a decimal point. Variable numberFive is invalid because of this. Variable one is invalid because you’re trying to store an integer in a String variable. The compiler won’t let you store a value in a variable that doesn’t match the type (or subtype, for that matter, but that’s for another post). Lastly, the variables numberOne, numberTwo, and numberThree are all valid, the 0, 0x and 0b tell Java that you’re storing an integer value in octal, binary, and hexadecimal format, respectively. We’ll look at that in a later post.

 

Variable Defaults

Variables, as I’m sure you know, can be defined either in the class, where they’re known as class or instance variables (depending if they are static or not), or in a function, in which case they are referred to as local variables. While the naming convention remains the same as above, there is one key difference between the two. See if you can tell what’s wrong with this code below:

public class NumberTester {

     int myInstanceNumber;

     public void printNumbers(){
         int myLocalNumber;

         System.out.println(myInstanceNumber);
         System.out.println(myLocalNumber);
     }
}

The code won’t compile because of line 9. Why? Well, both of them are non-instantiated variables, but Java will assign a default value to a class level variable, but not to a local variable. If this code was run (by removing the offending line), myInstanceNumber is assigned a default value of 0.

That’s a quick primer on some of the fun ways you can break variables (or not) in Java. Up next, a bit more in depth look at variables that store numbers. The excitement never stops!

3 thoughts on “Java: Variable Naming

  1. Alex January 20, 2018 / 6:46 pm

    You really make it appear so easy along with your presentation but I to find this matter to be really one thing which I believe I might by no means understand. It seems too complicated and very huge for me. I’m taking a look ahead in your subsequent put up, I will try to get the hold of it!

    Like

  2. Alex January 20, 2018 / 9:33 pm

    I’m now not certain where you’re getting your info, however good topic. I needs to spend some time finding out more or working out more. Thank you for magnificent info I used to be on the lookout for this info for my mission.

    Like

  3. Leslie Campos January 22, 2018 / 4:36 am

    I appreciate, cause I discovered just what I was having a look for. You have ended my four day long hunt! God Bless you man. Have a great day. Bye

    Like

Leave a comment