Variables

Course Home

In the previous lesson we explored types. In order to use these types in our code, and any other objects that we’ve made or are getting from libraries, we need to create a new instance and give them a name. The name we give them is called a variable.

Let say we have a class called Person. We want to have three properties about a person - their first name, age, and height. We know what those types are going to be, so we can now create those properties in code as variables.

String firstName;
int age;
double height;

Now if I’d written all this code out, they’d be in a class called Person. The name of the class is important, we should use that to help us name the variables, so they are contextual. Naming variables in code is very important, depending on the context. If you are working on a project with other people, you’ll want them to be able to glance at a variable and immediately know what it is. If you are just hacking away to learn, then names aren’t really important.

The code above would just create some variables, but they have no values. We assign them values by using the = symbol.

String firstName = "Richard";
int age = 16;
double height = 142.6;

An important lesson that took me a while to grasp is to not over think variable names. If something is good enough, run with it. The IDEs are so powerful these days that changing a variable name later on in a program’s life is very easy.

Next Lesson ➔

Richard Bradshaw's photo
Author

Richard Bradshaw

@FriendlyTester

Software Tester, speaker and trainer at Friendly Testing. BossBoss at Ministry of Testing. Whiteboard Testing creator. Striving to improve the testing craft.

Comments