- Java Programming Basics
- Java Tutorial
- Java Overview
- Java Environment Setup
- Java Program Structure
- Java Basic Syntax
- Java First Program
- Java Constants
- Java Separators
- Java Keywords
- Java Data Types
- Java Data Types
- Java Integers
- Java Floating Point
- Java Characters
- Java Booleans
- Java Numbers
- Java Programming Variables
- Java Variables
- Java Variable Types
- Java Variable Scope
- Java Type Conversion
- Java Type Casting
- Java Auto Type Promotion
- Java Type Promotion Rules
- Java Programming Arrays
- Java Arrays
- Java One Dimensional Array
- Java Multidimensional Array
- Java Programming Operators
- Java Operators
- Java Arithmetic Operators
- Java Increment Decrement
- Java Bitwise Operators
- Java Left Shift
- Java Right Shift
- Java Relational Operators
- Java Boolean Logical Operators
- Java Ternary(?) Operator
- Java Operator Precedence
- Java Control Statements
- Java Decision Making
- Java if if-else if-else-if
- Java switch Statement
- Java Loops
- Java while Loop
- Java do-while Loop
- Java for Loop
- Java for-each Loop
- Java Nested Loops
- Java break Statement
- Java continue Statement
- Java Class Object Method
- Java Classes and Objects
- Java Class
- Java Object
- Java new Operator
- Java Methods
- Java Constructors
- Java this Keyword
- Java Stack
- Java Overloading Recursion
- Java Method Overloading
- Java Constructor Overloading
- Java Object as Parameter
- Java Call by Value Reference
- Java Returning Objects
- Java Recursion
- Java Modifier Types
- Java Encapsulate Poly String
- Java Encapsulation
- Java Polymorphism
- Java Nested Inner Class
- Java Strings
- Java Command Line Arguments
- Java Variable Length Arguments
- Java Inheritance Abstraction
- Java Inheritance
- Java super Superclass
- Java Multilevel Hierarchy
- Java Method Overriding
- Java Abstraction
- Java Packages Interfaces
- Java Packages
- Java Access Protection
- Java Import Statement
- Java Interfaces
- Java Programming Exceptions
- Java Exception Handling
- Java try catch
- Java throw throws
- Java finally Block
- Java Built In Exceptions
- Java Exception Subclasses
- Java Chained Exceptions
- Java Multithreading
- Java Multithreading
- Java Thread Model
- Java Main Thread
- Java Create Thread
- Java Thread Priorities
- Java Synchronization
- Java Inter Thread Communication
- Java Suspend Resume Stop Thread
- Java Get Thread State
- Java Enum Autobox Annotation
- Java Enumerations
- Java Type Wrappers
- Java Autoboxing
- Java Annotation
- Java Marker Annotations
- Java Single Member Annotation
- Java Built In Annotations
- Java Type Annotations
- Java Repeating Annotations
- Java Data File Handling
- Java Files I/O
- Java Streams
- Java Read Console Input
- Java Write Console Output
- Java PrintWriter Class
- Java Read Write Files
- Java Automatically Close File
- Java Programming Advance
- Java Date and Time
- Java Regular Expressions
- Java Collections Framework
- Java Generics
- Java Data Structures
- Java Network Programming
- Java Serialization
- Java Send Email
- Java Applet Basics
- Java Documentation
- Java Programming Examples
- Java Programming Examples
Java Variable Types
Variable is basically a name of the memory location. Based on the data type of variable, memory layout and size can be different.
Here the following table lists all the three types of variables available in Java along with their meaning:
Variable Type | Meaning |
---|---|
Local Variables | Local variables Java, are created locally, such as inside the method, constructor, or any block and these local variables automatically destroyed upon the exits from that method, constructor, or any block where they are created |
Instance Variables | Instance variables in Java, are created whenever an object is created through the use of new operator. You can create instance variables in Java, in a class, but outside any block, method, or any constructor. |
Static Variables | Static variables are also called as class variables, are created through the use of static keyword in a class, but outside any block, method, or any constructor |
Local variables in Java
Here is an example program, demonstrates the concept and use of local variables in Java. Here marks is the local variable.
/* Java Variable Types - Java Local Variables - Example Program */ public class JavaProgram { void printMarks() { int marks = 0; marks = marks + 96; System.out.println("Marks of Deepak = " + marks); } public static void main(String[] args) { JavaProgram jp = new JavaProgram(); jp.printMarks(); } }
Here is the sample output produced by the above Java program:
Instance Variables in Java
Here is an example program, illustrates the concept and use of instance variables in Java:
/* Java Variable Types - Java Instance Variables - Example Program */ import java.io.*; public class JavaProgram { public String studname; // this instance variable is visible for any child class private String studbranch; // this instance variable is only visible to the JavaProgram class public JavaProgram(String name) { studname = name; } public void initializeBranch(String branch) { studbranch = branch; } public void printStudent() { System.out.println("Name = " + studname); System.out.println("Branch = " + studbranch + "\n"); } public static void main(String args[]) { JavaProgram jp1 = new JavaProgram("Deepak"); jp1.initializeBranch("CSE"); jp1.printStudent(); JavaProgram jp2 = new JavaProgram("Rajat"); jp2.initializeBranch("Electrical"); jp2.printStudent(); } }
Here is the sample output produced by this Java Program:
Static Variables in Java
Here is an example program, helps you in understanding the static variables in Java:
/* Java Variable Types - Java Static Variables - Example Program */ import java.io.*; public class JavaProgram { private static String studname; // this is a private, static variable public static final String studbranch = "IT"; // constant variable public static void main(String args[]) { studname = "Deepak"; System.out.println(studname + " is in " + studbranch + " branch"); } }
The above Java program will produce the following output:
« Previous Tutorial Next Tutorial »