Androidberry logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Androidberry Facebook connect
  • Androidberry twitter connect
  • Androidberry Google Plus connect

Java Tutorials


Basic Introduction

What is Java?
Java Features
Java vs Other Languages
Java on Internet
Java Components
Java First Program
Java Commandline argument
Java Constants
Java Variables
Java Types of Variables
Java Datatypes
Java Getting Input from User
Java Typecasting
Java Operators
Java Math Functions

Decision Making

Java if statement
Java if-else statement
Java nested if-else
Java else-if ladder
Java Switch case statement
Java Nested Switch Case
Java Ternary/Conditional operator

Looping

Java while loop
Java do-while loop
Java for loop
Java Enhanced loop/for each loop
Java Labelled for loop
Java Nesting of loops
Java Break and Continue

Classes and Objects

Inheritance

Interfaces

Packages

Exception Handling

Multithreading

Java Applets

Graphics Programming

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Java Variable Types

In Java, we can declare the variables in different ways based on their use in the program.


Java Variable Types

Types of variables in java

There are 3 types of variables in Java

  • Instance variables
  • Local variables
  • Class variables (static variables)

Instance Variable

What is instance variable

  • These variables are declared inside the class but not in method like constructor.
  • They are created when the objects are created using new keyword.
  • Their values are referenced by methods
  • They are always visible to all methods or block of code.
  • They have default values which is assigned at the time of creation.

Example - Displaying laptop details using instance variables.

public class InstanceVar 
{
    String Brand = "HP";
    String series ="Spectre";
    float screen_size = 34.5f;
    byte ram_size = 8;
	
    void display()
    {
	System.out.println("Brand Name : "+Brand);
	System.out.println("Series : "+series);
	System.out.println("Screen size : "+screen_size+" cm");
	System.out.println("RAM Size : "+ram_size+" GB");
    }
	
    public static void main(String[] args) 
    {
	InstanceVar iv = new InstanceVar();
	iv.display();
    }
}

Output

Brand Name : HP
Series : Spectre
Screen size : 34.5 cm
RAM Size : 8 GB

Class | Static variables

What is class variable?

  • They are also known as static variables because of declaring with static keyword.
  • It belongs to all objects created by class.
  • All the properties of static keyword are applied to class variables.

Example - Displaying details of customer using class variables.

public class ClassVar 
{
    static String RestName = "Eleven Madison Park"; // static variable
    String cust_name; // instance var
    int totalOrders; // instance var
	
    void display()
    {
	System.out.println("\nRestaurant : "+RestName);
	System.out.println("Customer Name : "+cust_name);
	System.out.println("Total Orders : "+totalOrders);
    }
	
    public static void main(String[] args) 
    {
	ClassVar c1 = new ClassVar();
	ClassVar c2 = new ClassVar();
	ClassVar c3 = new ClassVar();
		
	c1.cust_name = "Niko"; 
	c1.totalOrders = 3;
		
	c2.cust_name = "Alex";
	c2.totalOrders = 2;
		
	c3.cust_name="Emma";
	c3.totalOrders = 2;
		
	c1.display(); c2.display(); c3.display();
    }
}

Output


Restaurant : Eleven Madison Park
Customer Name : Niko
Total Orders : 3

Restaurant : Eleven Madison Park
Customer Name : Alex
Total Orders : 2

Restaurant : Eleven Madison Park
Customer Name : Emma
Total Orders : 2


Local Variables

What is Local variable?

  • Local variables declared and used inside the methods.
  • They have limited scope within that block only.
  • They are not available outside of the method definition.
  • Local variables can be declared with same name in each method.
  • They does not allow the global access.
NOTE : Java doesn't support global variables like in C and C++

Example - Local variable program in java

public class LocalVar 
{
    void display(int getPrice,String itemName) // local var
    {
	System.out.println("Item name : "+itemName);
	System.out.println("Price : "+getPrice);
    }
    void display(String itemName, int getPrice) // local var
    {
	System.out.println("Item name : "+itemName);
	System.out.println("Price : "+getPrice);
    }

    public static void main(String[] args) 
    {
	LocalVar local = new LocalVar();
	local.display(1200, "Travel Bag");
	local.display("Glasses", 600);
    }
}

Output

Item name : Travel Bag
Price : 1200
Item name : Glasses
Price : 600

Scope of variables

Scope of the variables is nothing but the life time of a variable. It depends upon the where in the program that variable is declared.

“the area of the program where the variable is accessible is called its scope”.

In above types, the local variables have only limited scope within it's block.All other types of variables are accessed anywhere.


<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo