Unit 1 Variables
Unit 1 Variables
Unit 1 Variables
Syntax
default
private
public Name of the
protected variable
Three types
● Local variable
● Instance variable
● Class variable
Local variable
● variable defined within a block or method or constructor is called local variable.
● Created when the block in entered or the function is called
● Destroyed after exiting from the block or when the call returns from the
function.
● Initialization of the variable is mandatory
● The scope of these variables exists only within the block in which the variable is
declared. i.e. we can access these variable only within that block .
Instance variable
● A variable declared inside the class but outside the body of the method,
is called instance variable
● Instance variables are non-static variables
● Created when an object of the class is created
● Destroyed when the object is destroyed.
● Initialization is not mandatory. Its default value is 0
● Instance Variable can be accessed only by objects
Class or Static variables
● Variable which is declared as static is called static variable.
● It cannot be local.
● Method declaration
● Method definition
● Method invocation/Call
Method Declaration
Syntax
default
private
public Type of input
protected Identifier
variable
void show();
● access-specifier – default
● return-type – void
● methodName – show
● data type of arguments – nil
Example- 2
void show(String);
● access-specifier – default
● return-type – void
● methodName – show
● data type of arguments – String
Example- 3
Syntax
default
private
public Declaration of
protected Data type Identifier
input variable
method body;
}
Method example
Method example - 1
void show(){
System.out.println(“Object oriented programming”);
}
● access-specifier – default
● return-type – void
● methodName – show
● arguments – nil
Method example - 2
Syntax
methodName(argument value(s));
Example
show();
show(“Java Programming”);
Syntax
Name of Construct
the class or
ClassName object_name=new ClassName();
keyword
Example of an Object
Class with Object
Constructors
● Similar to method
● Method name is same as Class name
● No return type
● Can have arguments/parameters
● Cannot be invoked explicitly
● It is invoked when an instance of a class is created
● Can be overloaded
● Used to initialize the member of a class
● Two types
○ Default constructor
○ Parameterized constructor
Syntax for constructor
ClassName(argument(s) declaration) {
body of constructor;
}
Example
Class Example{
int ivar;
Example(){ //constructor
ivar=10;
}
public static void main(String[ ] args){
Example e1=new Example();
System.out.println(e1.ivar);
}
}
Types of constructors
● Default constructor
○ Constructor without parameter
● Parameterized constructor
○ Constructor with parameter
Example for Constructor
class Constructor{
Constructor(){
System.out.println(“Default constructor”);
}
Constructor(String str){
System.out.println(“Parameterized constructor”);
}
}
main() method
Syntax
}
Input statement
construct
object
or
Output statement
Method from
Syntax PrintStream
Class from class
lang package
System.out.print(“string value”);
final member of
System class of
type PrintStream
static keyword
● Variable
● Method
● Block
● Nested class
static variable
Syntax
● Method with static keyword
static return-type
method_name(arguments){
● Can be accessed by class name
method body;
}
● Access only static members
Example
● It will preserve the last value static void show(String str){
System.out.println(str);
● Belongs to class rather than the object of a class
}
static block
Syntax
● Block with static keyword
static{
statement(s);
● Executed prior to main() method
}
● Used to initialize static members
Example
static{
● Executed only once
System.out.println(“static
block ”);
}
Data types
Data types
Data Type Description Default Default size
Value
float Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits 0.0f 4 byte
3.4e−038 to 3.4e+038.
double Stores fractional numbers. Sufficient for storing 15 decimal digits 0.0d 8 byte
1.7e−308 to 1.7e+308
byte data type
Syntax
byte varname;
Syntax
short varname;
Syntax
int varname;
Syntax
long varname;
Syntax
float varname;
Syntax
double varname;