You are on page 1of 9

Types of Variable

Variables

Local Variables Global Variables

Global Static Variables Global non-static Variables


( Class variable ) ( Instance variable )
1) Local Variable
This variable declared inside of method/code block/constructor is called
local variable.
> Scope is within that block.

Public class ClassA {

public static void methodName()


{
String a = “Velocity”;

System.out.println(“ xyz ”);

}
}
2) Global Variable
This variable declared outside of method/code block/constructor and
inside the class is called global variable.
> Scope is throughout the class.

Public class ClassA {

String a = “Velocity”;

public static void methodName()


{

System.out.println(“ xyz ”);

}
}

Two Types -- > a) Static Variable b) Non-Static variable


a) Static Variables b) Non-Static Variables
Global variables with static keyword Global variables without static keyword
called static variables. called non-static variables.

Public class ClassA { Public class ClassA {

static String a = “Velocity”; String a = “Velocity”;

public static void methodName() public static void methodName()


{ {

System.out.println(“ xyz ”); System.out.println(“ xyz ”);

} }
} }
Global and local variable with a same name.
Public class ClassA {

String a = “Velocity”;

public void methodName()


{
String a = “Classes”

System.out.println(a);

System.out.println(this.a);

}
}
Constructor
1) It is a special type of method which is used to initialize the variables (data
member). public class ClassA {
 It is a special member of a class.
int a;
Key Point int b;
 Constructor name should be same as class name.
public ClassA()
 It should not have any return type. {
 Constructor must not be abstract, static and final. a = 10;
 To call constructor we have to use new keyword. b = 20;
}
Use
 To initialize data members of a class public static void methodName()
{
 To load non-static members of a class System.out.println(“ xyz ”);
(variables & Methods) }
}
Types

1) Default constructor
If constructor not declared in java class then at the time of compilation
compiler will provide default constructor.

Public class ClassA {

public static void methodName()


{

System.out.println(“ xyz ”);


How to call constructor ?
}
} By using new keyword
Ex. new ClassA();
2) User defined constructor
Constructor declared by programmer is called user defined constructor.

a) Zero argument constructor b) With argument constructor


public class ClassA { public class ClassA {

int a; int a;
int b; int b;

public ClassA() public ClassA(int k, int p)


{ {
a = 10; a = 10;
b = 20; b = 20;
} }

public static void methodName() public static void methodName()


{ {
System.out.println(“ xyz ”); System.out.println(“ xyz ”);
} }
} }
Constructor overloading

We can declare multiple constructor in a class with same name but


with different argument.
public class ClassA {
int a; public ClassA(int k, int p)
int b; {
a = 10;
public ClassA() b = 20;
{ }
a = 10;
b = 20; public ClassA(int p, int k)
} {
a = 10;
public ClassA(int k) b = 20;
{ }
a = 10;
b = 20;
}

You might also like