A variable in Java is a name of a memory location that can hold some value. Variables provide an easy way for programmers to access memory locations.
The word variable itself is a combination of “vary + able “, a value that can be changed.
Every Java variable has:
- A name
- A data type which specifies the size and layout of variable, memory location associated with it, range of values that the variable can hold and operations that can be safely applied on the variable.
- A value
DataType Name = Value
Consider for instance:
int foo = 70;
In the above single line of code, int is the data type of the variable foo and 70 is the value. This line defines a memory location that will store a value 70 and all operations that this data type int support can be applied to that variable.
Java variables naming conventions
Naming convention in Java is a way you call all your variables, classes and methods in an application.
There are a number of different naming conventions out there. No matter which one you choose, the most important part is to stick to it through your application.
The most popular naming convention among Java developers is CamelCase.
Java has the following three types of variables:
- Local variables
- Instance variable
- Static variable
Local Variables
These are variables that are declared inside a method, constructor or block. They are called local variables because they are accessible only within the declared method, constructor, or block not outside.
Local variables are created as soon as program control enters the block and are destroyed once the program control exits this block.
Java local variables best practices:
Do:
- Define local variables as close as possible to their first use.
- Prefer local variables to any other variables. They are short lived and are destroyed each time the program exits the block.
Don’t:
- Avoid methods with too many local variables. They are hard to read and should be refactored.
- Avoid using the same name for variables within one block.
Here is an example of implementing random integers with a specific range with local variables:
private int findResult() { int min = 1; int max = 10; int goal = 5; int randomNumber ) ThreadLocalRandom.current().nextInt(min, max); if(randomNumber == goal) { return randomNumber; } return -1; }
Instance variables
These are the variables that are declared inside a class but outside any method or constructor. They are created when an object is created by using the keyword new
and are destroyed when the object is destroyed.
These variables hold values that must be referenced by more than one method, constructor or block, or essential parts of an object’s state that must be present throughout the class.
The following code illustrates the concept of local and instance variables:
class Foo { private int bar; //instance variable public void setBar(int bar) { this.bar = bar; } public int calculateBar() { return bar*bar; } } public class InstanceVariables { public static void main(String[] args) { int localBar; //local variable Foo foo = new Foo(); foo.setBar(5); localBar = foo.calculateBar(); System.out.println(localBar); } }
In this example, we have exactly one instance variable in class Foo
that is an integer bar
.
Java instance variable best practices:
- Consider making all your instance variables private and access them through getters and setters. An instance variable should be just that.
- Apply naming conventions to all your instance variables. Do not mix different ways to call your variables. It will make your code difficult to read. Consistency is the key.
- Do not give your variables too short or too long names. 2-3 words is enough.
- Choose meaningful names for your instance variables. Getters and setters methods will be much easier to understand as a result of that. For example, call your variable
calculatedPrice
andmaxPrice
and notp1
andp2
. - Do not reuse variable names within the same class. It is confusing and may result in bugs that are difficult to spot.
- Do not use non-ASCII characters in your variables names. It is not guaranteed that they will work on every computer.
Static or Class Variables
Static variables in Java are variables that do not depend on any class instance. These are independent variables that are created as soon as the program starts and are destroyed when the program stops. Only one instance of a static variable is created during the program run. You may call them global as well.
Static variables are declared using the static
keyword and are placed outside of any method, constructor or a block.
The code below illustrates the concept of a Static Variable.
class FooBar { private static int foo; private int bar; public void setFoo(int input) { foo = input; } public void setBar(int bar) { this.bar = bar; } public void print() { System.out.println("foo: "+foo); System.out.println("bar: "+bar); } } public class StaticVariables { public static void main(String[] args) { FooBar fooBar = new FooBar(); fooBar.setFoo(1); fooBar.setBar(2); fooBar.print(); } }
The output of the above program is:
foo: 1
bar: 2
Java static variables special features:
- You do not reference a static variable with a keyword
this
. - Anything can modify a static variable.
- It’s a one in a lifetime (or global) variable.
- It’s value is consistent between all instances of an object.
- Static variables are generally considered to be “evil” variables because of the above features.
See Also
- - Java regex by example: strings
- - Java Basic Programs: 5 Simple Examples
- - Java 9 Strings
- - Java Hello World – How to Run
- - How to sort a Set in Java example