Java Variable Types - Java

What is Java Variable Types?

A variable offers us with named storage that our applications can control. every variable in Java has a particular type, which determines the size and layout of the variable's memory; the variety of values that may be stored in that memory; and the set of operations that can be applied to the variable.

You need to claim all variables before they can be used. Following is the basic form of a variable statement –

Here data type is one of Java's datatypes and variable is the name of the variable. To declare more than one variable of the specified type, you can use a comma-separated list.

Following are valid examples of variable declaration and initialization in Java –

Example

This chapter will explain various variable types available in Java Language. There are three kinds of variables in Java −

  • Local variables
  • Instance variables
  • Class/Static variables

Local Variables

  • Local variables are declared in methods, constructors, or blocks.
  • Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block.
  • Access modifiers cannot be used for local variables.
  • Local variables are visible only within the declared method, constructor, or block.
  • Local variables are implemented at stack level internally.
  • There is no default value for local variables, so local variables should be declared and an initial value should be assigned before the first use.

Example

Here, age is a local variable. This is defined inside pupAge() method and its scope is limited to only this method.

This will produce the following result −

Output

Example

Following example uses age without initializing it, so it would give an error at the time of compilation.

This will produce the following error while compiling it −

Output

Test.java:4:variable number might not have been initialized

Instance Variables

  • Instance variables are declared in a class, but outside a method, constructor or any block.
  • While an area is allocated for an item inside the heap, a slot for each instance variable value is created.
  • Instance variables are created when an item is created with the use of the keyword 'new' and destroyed when the object is destroyed.
  • Instance variables hold values that need to be referenced by using more than one approach, constructor or block, or important components of an item's country that ought to be present for the duration of the elegance.
  • Instance variables may be declared in the class level before or after use.
  • Access modifiers can be given for instance variables.
  • The instance variables are visible for all methods, constructors, and block in the class. Generally, it is encouraged to make these variables private (access level). But, visibility for subclasses may be given for these variables with the use of access modifiers.
  • Instance variables have default values. For numbers, the default value is 0, for Booleans it is false, and for object references, it's far null. Values can be assigned during the declaration or within the constructor.
  • Instance variables may be accessed directly by calling the variable call inside the class. However, within static methods (whilst instance variables are given accessibility), they should be called the use of the fully qualified name. ObjectReference.VariableName.

Example

This will produce the following result −

Output

Class/Static Variables

  • Class variables also referred to as static variables are declared with the static keyword in a class, however outside a method, constructor or a block.
  • There might only be one copy of every class variable per class, regardless of how many items are created from it.
  • Static variables are hardly ever used aside from being declared as constants. Constants are variables which are declared as public/private, final, and static. Constant variables never change from their initial value.
  • Static variables are saved inside the static memory. It is rare to apply static variables other than declared final and used as either public or private constants.
  • Static variables are created when the program starts and destroyed when this system stops.
  • Visibility is much like instance variables. But, maximum static variables are declared public considering that they need to be to be had for users of the class.
  • Default values are identical as example variables. For numbers, the default value is zero; for Booleans, it is false; and for object references, it is null. Values may be assigned all through the declaration or within the constructor. Additionally, values may be assigned in unique static initializer blocks.
  • Static variables may be accessed by calling with the class name ClassName.VariableName.
  • When declaring class variables as public static final, then variable names (constants) are all in upper case. If the static variables aren't public and final, the naming syntax is similar to instance and local variables.

Example

This will produce the following result −

Output

Note − If the variables are accessed from an outside class, the constant should be accessed as Employee.DEPARTMENT

What is Following?

You already have used access modifiers (public & private) in this chapter. The next chapter will explain Access Modifiers and Non-Access Modifiers in detail.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java Topics