❌

Normal view

There are new articles available, click to refresh the page.
Before yesterdayMain stream

Variable

By: Prasanth
5 June 2025 at 07:06

what is Variable?

Variable(named memory location )are containers for storing data values.like number,words,or object,Each variables has a specific type ,it determines the size and layout of variable memory.

java keywords:-

java has a set of keywords that are reserved words that can not to used as variables,methods,classes or any other identifiers

Example:-

String nation="India";

String is keyword ,it indicates that the nation variable of String data type ,that variable hold string value particular memory location.

some keywords:-
abstract,int,byte,boolean,class,do,else,extedns,final,if,while....etc

what is identifier:-

Identifier in java is the name used to identify a class,interface,methods,variables,constructor,package...etc

only allowed to java is naming rules class,interface,methods,variables,constructor,package...etc

Rules with Naming Conventions:-

  • start with lowercase,then capital words.example studentName, rollNO.
  • Should be meaningful
  • can to be keyword.
  • avoid to $ and _
  • Can not start with digit and can contain letters
  • No space and symbols

Variable Declaration and initialization

you must declare all variables before the can be used. variables are declared by specifying data type depend upon the variable name.

syntax:-

<data_type> <variable_name> = <value>;

Example:

int a, b, c; // Declare variable (int data type, a,b,c variables)
int a = 10, b = 10; // value initialization
double pi = 3.14159; // declares and assigns a value
char a = 'a';
String name = "Prasanth";

Types:

Local variable:

  • Local variable declared inside the the body of method (including parameter method),constructor or block is called local variable and only visible inside not to be outer . you can use only this variable only with in the method and the other method do not aware that the variable is exit.It is a combination of "vary + able" which means its value can be changed.

  • Local variable there is no default value for local variables, so local variables should be declared and an initial value before first use.

  • Local variable can not be define with the static and other access modifier only allowed to final.

  • when method starts ,memory is given to local variable and method ends ,that automatically removed. Jvm is handle the memory allocation to local variable and when method start or called it stored(Location) or allocated stack memory ,method ends(Life time) stack frame is removed and local variable automatically destroyed.

  • scope means: where the variable can be used or accessed.

Instance Variable:-

  • Instance variables are known as non-static variables and are declared and visible(access modifier) in a class outside of any method,constructor,block.instance variable created when object is created.each object of the class has its own copy of instance variables.
  • you can use instance variable to access modifier unless it has default access modifier.Initialization of an instance variable not mandatory if you not initialization variable default value provide,dependent on the data type of variable.instance variable can be accessed only by creating object.instance variable are used ,when you need to share values b/w multiple methods or blocks in class and instance variable access to class level .
  • instance variable directly access or use the inside of instance variable without any reference and static method when you need to access or use you need to create object for example you need to object reference.variable name .
  • you can use initialization instance variable using constructor and instance blocks.
  • (Lifetime)
  • Memory for instance variable is allocated when object is created and memory is freed when object is destroyed
  • space allocated or memory location = an object in the heap.

Manage by = jvm - Garage collector

static Variable:-

  • static variable declared with the static keyword in a class, but outside method and constructor or a block.
  • Each variable are only one copy per class ,no matter how many object create from.
  • static variable rarely used to declaring constant (static+final), once you set can not to be change the initial value and static variable stored int static memory.

Example:

public class Car
{

public static final String  color= "red";
   public static void main(String args[]) {

String color = "orange";
}}

  • static variable are created or memory allocated when the programs starts ,when stop the programme stopped.
  • visibility similar to instance variable.
  • static variable Default value same as instance variable ,variey default values it depends upon datatype what you used in the variable.
  • static variable can be accessed by calling with class name Classname.Variablename or without class. without class name access static variable the compiler will automatically append the class name, but accessing static variable different class you must to mention classname.
  • you can access a static variable like instance variable to the object , but compiler will show warning message and it is not affect the program
  • static variable can not declare locally like inside method.and also static block can be used to initialize static variable.

Note:-

  • static variable share one copy for all object ,if changed ,it affect all
  • Non-static variable gives each object is own copy ,if changed anything one ,it not affect all.

we will see the program in upcoming blogs

❌
❌