Search this site
Embedded Files
arjava
  • Home
  • Program
    • Basic Java
    • Advance Java J2EE
    • Data Structure
    • Python
  • interviewPreparation
  • Downloads
    • Softwares
    • Study Material
  • Tutorial
arjava
  • Home
  • Program
    • Basic Java
    • Advance Java J2EE
    • Data Structure
    • Python
  • interviewPreparation
  • Downloads
    • Softwares
    • Study Material
  • Tutorial
  • More
    • Home
    • Program
      • Basic Java
      • Advance Java J2EE
      • Data Structure
      • Python
    • interviewPreparation
    • Downloads
      • Softwares
      • Study Material
    • Tutorial
Introduction
Language Fundamentals
Operators & Assignments
Flow Control
Declaration & Access Modifiers
Interface
OOP's Concept
Collection Frameworks
Generics
Multithreading
Serialization
Regular Expression
Enumeration
Internationalization
JDK JRE JVM
Garbage Collection
Assertions
Exception Handling
Important

Types of Variables -

Based on the type of value represented by a variable, all variables are divided into two types.

  1. Primitive Variable - can be used to represent primitive values int x = 10;
  2. Reference Variable - can be used to refer objects Student s = new Student( );

Based on the purpose and positions of declaration the variables are divided into three types.

  1. Instance Variable - also known as Object Level variable or attributes.
  • If the value of variable is varied from object to object such type of variables are called instance variable.
  • For every object a separate copy of instance variable will be created.
  • The scope of instance variable is exactly same as the scope of object, because instance variable will be create at the time of object creation and same for destroy.
  • Instance variable stores as the part of object.
  • Instance variable should be declare within the class directly but outside the method, block or constructor.
  • Instance variables cannot be access from static area directly, we have to use object reference, but from instance area we can access instance members directly.
  • It is not required to perform initialization explicitly, JVM always provides default values.

2. Static Variable - also known as Class Level variable or fields.

  • If the value of a variable is not varied from object to object such type of variables are called static variable.
  • Single copy creates at class level and the copy will be share with all objects of that class.
  • Static variable creates at the time of class loading and destroy at the time of class unloading, hence the scope of static variable is exactly same as the scope of class.
  • Static variable should be declare within the class directly but outside the method, block or constructor with static modifier.
  • Static variable can be access either by using class name or object reference, within the same class it is not required to use class name, we can access directly.
  • We can access from instance and static area directly just because of static variable creates at the time of class loading i.e., at the beginning of the program
  • It is not required to perform initialization explicitly, JVM always provides default values.
  • Static variables stores in method area.
  • If we are performing any change to static variable, these change will be reflect for all objects because only single copy shares all among objects.

3. Local Variable - also known as stack variable or temporary variable.

  • To meet temporary requirements of the programmer sometimes need to create variable inside method, block or constructor such type of variable is called local variable
  • Local variable stored inside stack.
  • Local variable creates while executing the block in which we declared it and destroy once the block completes, hence the scope of local variable is exactly same as the block.
  • For the local variable JVM won't provide any default values compulsory we have to perform initialization explicitly before using the variable.
  • The only applicable modifier for the local variable is final, if we use other modifier then we will get compile time error.

Note : compulsory initialize local variable at the time of declaration at least default value but No need to perform initialization of local variable inside logical block because there is no guarantee of execution of block.

var args method (1.5 version) -

Until 1.4 version, we can not declare a method with variable number of arguments, if there is any change in number of arguments compulsory we have to declare a new method, this approach increase the length of code and reduce readability.

To resolve this issue sun people introduced var arg method in 1.5 version, hence from 1.5 version onward we can declare a method with variable number of arguments such type of method called var arg method.

method(int... x)

we can invoke this method by passing any number of int values.

method( );

method(10, 20);

method(10);

method(10, 20, 30, 40);

main( )

whether the class contains main( ) or not, main( ) property is declared or not, this all are not going to check at compile time by compiler. JVM is responsible for this, if JVM unable to find required main( ) then we will get run time exception NoSuchMethodError : main

Public Static void main(String[ ] args)

Public - To call by JVM from anywhere

Static - without existing object, JVM can call this method

void - main method won't return anything to JVM

main - Name of the method which is configured inside JVM

String[ ] arg - Command Line Arguments

Back

Next

Google Sites
Report abuse
Google Sites
Report abuse