004) - Types of Variables and Constructor
004) - Types of Variables and Constructor
Variables
}
}
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.
String a = “Velocity”;
}
}
} }
} }
Global and local variable with a same name.
Public class ClassA {
String a = “Velocity”;
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.
int a; int a;
int b; int b;