Engineering Full Stack Apps with Java and JavaScript
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
Initialization along with declaration can be done as
int myVariable = 5;
You can initialize variables from initialization blocks or through constructors.
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
int i=5;
The primitive type int (primitive type for integer) tells that this variable can hold only integers.
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
myObject1.val=5;
myObject2 = myObject1;
myObject2.val = 10;
System.out.println(myObject1.val);
Output will be:
10
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.
class MyClass{
int myInsVar=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:
obj1.myInsVar++;
obj1.myStaVar++;
obj2.myInsVar++;
obj2.myStaVar++;
3. First calculate the output looking at the below code (without actually executing) and then execute to see if your findings were correct:
System.out.println(obj1.myInsVar);
System.out.println(obj1.myStaVar);
System.out.println(obj2.myInsVar);
System.out.println(obj2.myStaVar);
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.
Comments
need more exercise
need more exercise
do methods paramters come
do methods paramters come under local variables
yes method parameters are
yes method parameters are local to the method in which they are declared.
yes method parameters are
yes method parameters are local to the method in which they are declared.