Java Variable Types

Java Variable Types


Posted in : Core Java Posted on : September 28, 2010 at 12:39 PM Comments : [ 0 ]

This section contains the types of variable in java.

Java Variable Types

In Java all variable must be declared before it will be used anywhere in the program. The declaration syntax is as follows :

type identifier [ = value][, identifier [= value] ...] ;

Here 'type' is data type and 'identifier' is the variable name.

Java have following types of variable :

  • Local variables

  • Instance variables

  • Class/static variables

Local Variables

  • The variables which are declared inside methods, constructors, or blocks are known as local variables.

  • These variables have local scope i.e. it can't be access outside the program.

  • Need initial value assignment before use.

  • Access modifiers cannot be used for local variables.

Given below the sample code :

Main.java

package corejava;

public class Main {
	public void Age() {
		int age = 0;
		age = age + 7;
		System.out.println("Age: " + age);
	}

	public static void main(String args[]) {
		Main m = new Main();
		m.Age();
	}
}

In the above code 'age' is the local variable. The scope of this local variable is limited to method 'Age'.

Output

C:\Program Files\Java\jdk1.6.0_18\bin>javac Main.java

C:\Program Files\Java\jdk1.6.0_18\bin>java Main

Age: 7

Instance Variables

  • This method declared outside methods, constructors, or blocks .

  • When memory is allocated for object, the space slot for each instance variable value is created.

  • Instance variable created and destroyed with the creation and destruction of object.

  • More than one method , constructor or block can refer to the values hold by instance variable.

  • Access modifiers can be given for instance variables.

  • The default value of instance variable for numbers is 0, for Boolean it is false, and for object references it is null.

  • Inside class , you can access variable by calling variable name directly without using any object reference. But within static methods and different class, it should be called using object reference as : ObjectReference.VariableName

Example :

In the given below example, variable 'name' is instance variable :

public class Student {
// this instance variable is visible for any child class.
public String name;

// salary variable is visible in Student class only.
private int marks;

// The name variable is assigned in the constructor.
public Student(String StudName) {
name = StudName;
}

// The marks variable is assigned a value.
public void setMarks(int studMarks) {
marks = studMarks;
}

// This method prints the student's details.
public void printStud() {
System.out.println("Student name : " + name);
System.out.println("Marks :" + marks);
}

public static void main(String args[]) {
Student st = new Student("Divya");
st.setMarks(80);
st.printStud();
}
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac Student.java

C:\Program Files\Java\jdk1.6.0_18\bin>java Student

Student name : Divya
Marks :80

Class/Static Variable

  • Class variable also declared outside a method, constructor or a block with a 'static' keyword.

  • Class variable has only one copy for each class, regardless of how many objects are created from it.

  • Static variables are generally used as constant. Constants are variables that are declared as public/private, final and static. Constant variables never change from their initial value.

  • Class variable is created when program starts and ends with the program.

  • It has same default values as instance variable.

  • Static can only be accessed by using it's fully qualified name as : ClassName.VariableName

  • If you are declaring class variable as public static final, then variable(constant) should be in upper case. If the static variables are not public and final the naming syntax is the same as instance and local variables.

Example :

College.java

package net.roseindia;

public class College {
	// salary variable is a private static variable
	private static int resultPercent;

	// DEPARTMENT is a constant
	public static final String BRANCH = "Computer Science";

	public static void main(String args[]) {
		resultPercent = 76;
		System.out.println(BRANCH + " Result %:" + resultPercent);
	}
}

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>javac College .java

C:\Program Files\Java\jdk1.6.0_18\bin>java College

Computer Science Result %: 76

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics