A variable in Java hold some data.
As Java is a strongly typed language, we need to declare the type of a variable and the variable can only hold a compatible type of data.
Declaring the type of a variable is like introducing the variable to your program, and you should only introduce once.
The declaration of a variable begins with optional access modifier, the data type and is followed by the variable name and then a semicolon.
public int myVar;
Here, public is the access modifier and int is the data type of the variable named myVar.
Access modifiers tell which all code can access this member and access modifier public tell that this variable can be accessed from any other class.
The data type is the type of value that a variable can hold. The data type may be a primitive data type or a reference data type.
Initialization along with declaration can be done as
int myVariable = 5;
The data type may be a primitive data type or a reference data type.
A primitive data type is a basic type like int, float, double etc. and they hold a literal directly.
There are eight primitive data types defined in Java: boolean, byte, char, short, int, long, float, double.
Example: Primitive type
The primitive type int (primitive type for integer) tells that this variable can hold only integers.
Compatible types can be implicitely or explicitely casted to each other.
Except boolean, all other types are compatible with each other. A boolean can be assigned only a true or false or an expression that evaluate to true or false.
Example: Valid boolean assignments
boolean b1 = true;
boolean b2 = false;
boolean b3 = (3 < 5) ;
Example: Invalid boolean assignments
boolean b4 = 0;
boolean b2 = 1;
When an object is created using new keyword, enough memory is allocated in memory to hold all its members. A reference type variable holds the address of an object.
Example: Reference type
Consider a class MyClass:
We can create two objects of the class MyClass and assign it to the reference type as:
public MyClass myObject1 = new MyClass();
public MyClass myObject2 = new MyClass();
Here MyClass is the reference type and myObject1 and myObject2 are reference type variables that refer to an object of type MyClass, returned by "new MyClass()".
When you copy variables or when you pass a variable to a method in Java, always, the value is passed: Java will pass whatever the variable holds.
In the case of a primitive, Java passes the value of the variable and in the case of a reference type variable, it passes the address (reference) that is holds.
Example: Pass by value demonstration
myObject2 = myObject1;
myObject2.val = 10;
Output will be:
This is because, after the statement 'myObject2 = myObject1;' both reference type variables point to the same object in memory.
The object previously pointed by myObject2 doesn't have any reference to it and hence it will be eligible for garbage collection.
Any reference type can be assigned with a special literal called null.
This means that the reference is not pointing to an actual object of its type.
If you try to invoke a method on a null object, you will get NullPointerException.
1. Create a class MyClass with one instance variable myInsVar and one static variable myStaVar and initialize both to 0.
static int myStaVar=0;
2. In the main method of the same class, create two objects of MyClass.
MyClass obj1 = new MyClass();
MyClass obj2= new MyClass();
3. Increment both the variables on the objects as below:
3. First calculate the output looking at the below code (without actually executing) and then execute to see if your findings were correct:
Did you get the output as you expected? If yes, you have understood. Else remember that myStaVar is static and is common for all objects and there is only one copy per class, but there is a copy of myInsVar per object. Now try to understand it again. Still not able to understand, please feel free to ask me.
You can read more about access modifiers at access-modifiers-and-inheritance-in-java-with-examples.
Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!