Java Variables
-
Variable is a container which stores some data/value.
-
It has a name, type, and value.
Example:
public class HelloWorld { public static void main(String[] args) { /** * here below myVariable is the variable name. * int is the variable type. * 10 is the variable value. */ int myVariable = 10; System.out.println(myVariable); // Output: 10 } } -
Each variable has some specific type, which tells/determines the size and structure of the variable memory.
Example:
public class HelloWorld { public static void main(String[] args) { int myInt = 10; // 'myInt' is a variable of type 'int' double myDouble = 3.14; // 'myDouble' is a variable of type 'double' String myString = "Hello"; // 'myString' is a variable of type 'String' System.out.println(myInt); // Output: 10 System.out.println(myDouble); // Output: 3.14 System.out.println(myString); // Output: Hello } } -
There are 3 types of variables: local variable , instance variable , static variable .
Local Variable:
-
The variable that is declared inside a method, constructor, or block are called local variables.
Example-1:
Variable declared inside the method
public class HelloWorld { public void myMethod() { // This is a local variable which is declared inside the method 'myMethod' int localVariable = 10; System.out.println(localVariable); // Output: 10 } }Example-2:
Variable declared inside the constructor
public class HelloWorld { public HelloWorld() { // This is a local variable which is declared inside the constructor 'HelloWorld' int localVariable = 10; System.out.println(localVariable); // Output: 10 } }Example-3:
Variable declared inside the block
public class HelloWorld { public void myMethod() { { // This is a local variable which is declared inside the block int localVariable = 10; System.out.println(localVariable); // Output: 10 } } } -
These variables are created in the memory when the method, constructor, or block is executed and destroyed when the execution is finished. Hence the scope of the local variable is within the block, method or constructor only.
Example:
public class HelloWorld { public void myMethod() { int localVariable = 10; // local variable is created when the method is executed System.out.println(localVariable); // Output: 10 } // local variable is destroyed when the execution of the method is finished //At this line localVariable we can not access because it is destroyed after the execution of the method is finished. } -
Access Modifier can not be used with the local variable except final keyword.
Example:
public class HelloWorld { public void myMethod() { // 'final' access modifier can be used with local variable /** * access modifier like 'public', 'private', 'protected' can not be used with local variable * if we try to use any access modifier with local variable, it will give a compile-time error. * error - Illegal modifier for parameter localVariable; only final is permitted */ final int localVariable = 10; System.out.println(localVariable); // Output: 10 } } -
It can not be initialized by JVM, so we have to initialize it explicitly before using it.
Example:
public class HelloWorld { public void myMethod() { int localVariable; // local variable is declared but not initialized // System.out.println(localVariable); // This statement will give a compile-time // error - The local variable localVariable may not have been initialized. localVariable = 10; // local variable is initialized System.out.println(localVariable); // Output: 10 } } -
It can be accessed only within the method, constructor, or block where it is declared, can not be accessed outside of it.
Example:
public class HelloWorld { public void myMethod() { int localVariable = 10; System.out.println(localVariable); // Output: 10 } public void anotherMethod() { // System.out.println(localVariable); //Here localVariable we can not access. // This statement will give a compile-time error - Cannot find symbol } } -
It can not be declared as static because it is associated with an instance of the class.
Example:
public class HelloWorld { public void myMethod() { // static int localVariable = 10; // This statement will give a compile-time error - Illegal modifier for parameter localVariable; only final is permitted } } -
It is created in stack memory.
Instance Variable:
-
This variable is defined inside the class but outside the method, block and constructor. It is also called non-static variable.
Example:
public class HelloWorld { /** * This is an instance variable which is defined inside the class * but outside the method, block and constructor */ int instanceVariable = 10; public void myMethod() { System.out.println("myMethod"); } } -
Memory will be allocated for these variables when Object is created. So Multiple copies of memory will be allocated for multiple objects , because each object will have its own copy of instance variable.
Example:
public class HelloWorld { public static void main(String[] args) { MyClass obj1 = new MyClass(); // Memory will be allocated for instanceVariable of obj1 (copy one) MyClass obj2 = new MyClass(); // Memory will be allocated for instanceVariable of obj2 (copy two) } } class MyClass { // Memory will be allocated for this instanceVariable when Object is created int instanceVariable = 10; public void myMethod() { System.out.println("myMethod"); } } -
All the access modifiers can be used with this variable.
Example:
public class HelloWorld { // 'public' access modifier is used with instance variable public int publicInstanceVariable = 10; // 'private' access modifier is used with instance variable private int privateInstanceVariable = 20; // 'protected' access modifier is used with instance variable protected int protectedInstanceVariable = 30; // default access modifier is used with instance variable int defaultInstanceVariable = 40; // 'final' keyword is used with instance variable final int finalInstanceVariable = 50; public void myMethod() { System.out.println("myMethod"); } } -
This scope of the variable is inside the class and all the members.
Example:
public class HelloWorld { int instanceVariable = 10; // instance variable is declared inside the class but outside the method, block and constructor public void myMethod() { System.out.println(instanceVariable); // instance variable can be accessed inside the method } public void anotherMethod() { System.out.println(instanceVariable); // instance variable can be accessed inside another method } } -
Instance variables have some default value assigned by JVM when we do not initialize it explicitly. The default value of instance variable depends on the data type of the variable. For example, the default value of int is 0, double is 0.0, boolean is false, and reference types (like String) is null.
Example:
public class HelloWorld { int intInstanceVariable; // default value is 0 double doubleInstanceVariable; // default value is 0.0 boolean booleanInstanceVariable; // default value is false String stringInstanceVariable; // default value is null public void myMethod() { System.out.println(intInstanceVariable); // Output: 0 System.out.println(doubleInstanceVariable); // Output: 0.0 System.out.println(booleanInstanceVariable); // Output: false System.out.println(stringInstanceVariable); // Output: null } } -
It can be accessed by creating an object of the class and also can be accessed by its name with the class.
Example-1:
Accessing instance variable by creating an object of the class
public class HelloWorld { public static void main(String[] args) { MyClass obj = new MyClass(); // creating an object of the MyClass System.out.println(obj.instanceVariable); // instance variable can be accessed by creating the object of the MyClass } class MyClass { // instance variable is declared inside the class but outside the method, block and constructor int instanceVariable = 20; } }Example-2:
Accessing instance variable by its name with the class
public class HelloWorld { public static void main(String[] args) { System.out.println(MyClass.instanceVariable); // instance variable can be accessed by its name with the class } class MyClass { // instance variable is declared inside the class but outside the method, block and constructor int instanceVariable = 20; } } -
It can not be declared as static because it is associated with an instance of the class and Its can not be accessed from static context.
Example-1:
public class HelloWorld { static int instanceVariable = 10; // This will give a compile-time error - Illegal modifier for variable instanceVariable; only final is permitted public void myMethod() { System.out.println("myMethod"); } }Example-2:
public class HelloWorld { int instanceVariable = 10; // instance variable //static method static void display() { System.out.println(instanceVariable); // This will give a compile-time error - Cannot make a static reference to the non-static field instanceVariable } } -
It is created in heap memory.
Static Variable/Class Variable:
-
Static variables are declared with the static keyword inside the class but outside the method, block and constructor.
Example:
public class HelloWorld { // This is a static variable which is defined inside the class but outside the method, block and constructor static int staticVariable = 10; public void myMethod() { System.out.println("myMethod"); System.out.println(staticVariable); // static variable can be accessed inside class member like method, block and constructor } } -
Memory will be allocated for static variables when the class is loaded into the memory by JVM, It can be accessed by its class name, and it will be shared among all the objects of the class.
Example:
public class HelloWorld { public static void main(String[] args) { /** * static variable can be accessed by its class name (MyClass.staticVariable) * without creating the object of the class */ System.out.println(MyClass.staticVariable); // output: 10 } } class MyClass { // Memory will be allocated for this static variable when the class is loaded into the memory by JVM static int staticVariable = 10; public void myMethod() { System.out.println("myMethod"); } } -
Like Instance variable static variable aslo have some default value assigned by JVM when we don't initialize it explicitly. The default value of static variable depends on the data type of the variable. For example, the default value of int is 0, double is 0.0, boolean is false, and reference types (like String) is null.
Example:
public class HelloWorld { static int intStaticVariable; // default value is 0 static double doubleStaticVariable; // default value is 0.0 static boolean booleanStaticVariable; // default value is false static String stringStaticVariable; // default value is null public void myMethod() { System.out.println(intStaticVariable); // Output: 0 System.out.println(doubleStaticVariable); // Output: 0.0 System.out.println(booleanStaticVariable); // Output: false System.out.println(stringStaticVariable); // Output: null } } -
The scope of the variable is inside the class and all the members.
Example:
public class HelloWorld { static int staticVariable = 10; // static variable is declared inside the class but outside the method, block and constructor public void myMethod() { System.out.println(staticVariable); // static variable can be accessed inside the method } public void anotherMethod() { System.out.println(staticVariable); // static variable can be accessed inside another method } } -
Static variables are often used to store information that is shared among all instances of a class, like constants whose value does not change across different objects.
Example:
class Employee { int empId; String name; // this variable is shared by all employees, that is MyCompany is the same for all employees static String company = "MyCompany"; // Employee constructor Employee(int empId, String name) { this.empId = empId; this.name = name; } //display method to display employee details void display() { System.out.println(empId + " " + name + " " + company); } } public class HelloWorld { public static void main(String[] args) { Employee e1 = new Employee(101, "Rahul"); Employee e2 = new Employee(102, "Priya"); //So here for both employee e1 and e2, the company name is same. e1.display(); // output: 101 Rahul MyCompany e2.display(); // output: 102 Priya MyCompany } }
Note : Without static, each employee object will store its own copy of the company name, wasting memory. with static, all employee objects share the same company name, saving memory and ensuring consistency.
Java Identifiers
In Java, identifiers are unique names which are used with variables, methods, classes, interfaces, and packages, to identify them uniquely in the program.
Example:
package com.java.first.program; // here com, java, first, program are identifiers for package name
class Students { //Students is an identifier for class name
String name; // name is an identifier for variable name
int age; // age is an identifier for variable name
void show() { // show is an identifier for method name
int marks = 90; // marks is an identifier for variable name
}
}-
An identifier can contain letters, digits, underscore, and dollar sign.
Example:
class Students { String name ; String name1 ; String _name ; String $name ; String first_name ; } -
Cannot start with a digit.
Example:
class Students { String 5name ; // invalid } -
Cannot use Java keywords.
Example:
class Students { String class ; // Invalid because "class" is a Java keyword } -
It is Case-sensitive.
Example:
class Students { //Here bothe variable are different. int myage; int MyAge; } -
No spaces allowed.
Example:
class Students { int My Age; //This is invalid because it contains a space. }
Multiple Variable Declaration and Assignment
-
You can declare more than one variable in a single line:
Example:
class Students { int a, b, c; } -
Assigning Same Value to Multiple Variables
Example:
class Students { int a, b, c; a = b = c = 10; } -
Assigning Different Values
Example:
class Students { int a = 10, b = 20, c = 30; }
See you in next page....🙂