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:
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.
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:
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.
Do:
Don’t:
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; }
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:
calculatedPrice
and maxPrice
and not p1
and p2
.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:
this
.Senior Software Engineer developing all kinds of stuff.