Tutorials Hut

Java Variable Types

Java variable types are used to hold different java data types, on these variables we can perform various operations and manipulations for example mathematical operations (addition, subtraction, multiplication etc.) and any user defined operation.

As we already saw some details about different variables in java, in this article we will go more in detail to better understand this topic.

These java variable types, based on declared data type, can take different memory, you can check our article on java data type to understand their detail and memory requirement if needed.

Variables may be declared and later initialized and used or while declaration we may initialize them as well.

Generic syntax of java variable declaration:

1) Variable declared without initial value:

<Variable data type> <Name of variable>;

2) Variable declared with initial value:

<Variable data type> <Name of variable> = <Variable initial value/initialize> ;

Examples of java variables:

int a; 	         //Just declared variable a of data type int
int a, b, c;     //Just declared variable a, b and c of int type
int a=20;        //Variable a declared and assigned value 20
int a=30, b=45, c=90;    //Variable a, b and c declared and assigned
double amount=2.34d //double java data type variable declared
char a = ‘x’        //Variable a initialized with letter ‘x’

Below are there different java variable types are based on their declaration and position in a java class:

1) Local Variables
2) Instance Variables
3) Class/Static Variables

Local Variables

Local variables are declared inside a method, constructor or a block, as the name suggests they are local which means they will be accessible to that local scope only and can’t be accessed outside. Now, what do we mean by local scope? it means that if a variable is declared inside a method its existence will be within method and so on.

  • Local variables reside inside a method, constructor or block of code ({})
  • Local variables can’t be accessed outside their scope.
  • Local variables don’t have default value so they should be initialized with some value if needed.
  •  Local variables are saved in stack memory.
  • Local variables are the best options for any calculation or operation.
Example of local variables:
/*
Class showing some local variables
 */
class Garden{
 
   void myLocalVariablesMethod() {
   	//Examples of local variables
   	int years = 20;
   	float amount = 20.0f;
   	char firstChar = 'c';
 
   	Integer weight = 20;
   	Float price = 20.0F;
   	String name = "FlowersPark";
 
   	System.out.println("In local variables method, printing them name="+name+" years="+years);
   }
	public static void main(String args[]) {
    	Garden garden = new Garden();
    	garden.myLocalVariablesMethod();
	}
}

Output of above program:

In local variables method, printing them name= FlowersPark years=20

Instance Variables

  • Instance variables are declared in a class but outside method, constructor or a block.
  • Instance variables are accessible in the entire class, in a method or constructor or block everywhere.
  • Instance variables should be declared if they are used by methods or blocks of codes in a class.
  • Access modifiers can be added to them.
  • Instance variables have a default value based on data types, if you want to check data types and their default value may read our article java data types.
    For example int will have 0 default value boolean will have false and so on.
  • By calling names of variables we can access them in a method or a block.

Example:

/*
Class showing instance variables
 */
class Garden{
	//Below are examples of instance variables
	int years = 20;
	String name = "FlowersPark";
 
   void printDetailsOfGarden() {
   	//See below instance variables can be accessed in this method with name
 
   	System.out.println("Printing some of instance variables, name="+name+" years="+years);
   }
	public static void main(String args[]) {
    	Garden garden = new Garden();
    	garden.printDetailsOfGarden();
	}
}

Output of above program:

Printing some of instance variables, name=FlowersPark years=20

Static or Class Variables

  • Class or static variables are similar to instance variables but they are declared with a static keyword.
  • Static variables are accessible across class and also outside the class by just referring to them with class name.
  • Static variables will hold the same value across JVM memory and whenever accessed the latest value will be picked.
  • If concurrent threads or codes are updating and changing it the latest one will be always available.
  • Default values will be the same as default values of any java data types for example int will be initialized with 0.
  • You can easily access static variables by <Class name>.<Variable name>.
  • These can be declared with access modifiers (public, private etc.) which means private ones won’t be accessible outside.

Example:

/*
Class showing static/class variables
 /
class Garden{
	//Below are examples of static/class variables
	private static int years = 20; //outside class cant be accessed
	static String name = "FlowersPark";
 
   void printDetailsOfGarden() {
   	/*See below use of static/class variables, see below they are accessed with        class, this is more useful if you access from another class
   	here they may be accessed with just variable name, but to show you we have done this. */
   	System.out.println("Printing some of static variables, name="+Garden.name+" years="+Garden.years);
   }
	public static void main(String args[]) {
    	Garden garden = new Garden();
    	garden.printDetailsOfGarden();
	}
}

Output of above program:

Printing some of static variables, name=FlowersPark years=20

If you missed our previous article worth reading it : Java Data Types

References


















  • Leave a Reply

    Your email address will not be published. Required fields are marked *