Professional Documents
Culture Documents
Local Variables:
Defined inside methods, constructors or blocks
The variable will be declared and initialized within the method
The variable will be destroyed when the method has completed.
}
Compiled by : Haileyesus Demissie Object Oriented Programming 14
Types of Constructor
There are two types of constructors in Java:
Default constructor (No argument constructor)
Parameterized constructor
class Bike {
Bike(){
System.out.println("Bike is created");
}
public static void main(String[] args){
Bike b1 = new Bike();
}
}
Compiled by : Haileyesus Demissie Object Oriented Programming 16
Default Constructor
If there is no constructor in a
class, compiler automatically
creates a default constructor.
The default constructor is
used to provide the default
values to the object like 0,
null, etc., depending on the
data type.
Import java.net.*;
After the assignment, the value of std3 is the same as std2. Both
reference the same object Student.
♠ std3 ≠ std1
Output
Output
Output
Compiled by : Haileyesus Demissie Object Oriented Programming 53
Public
Public: The public access modifier is specified using the keyword public.
The public access modifier has the widest scope among all other access
modifiers.
Classes, methods or data members which are declared as public are accessible
from every where in the program.
There is no restriction on the scope of a public data members.
Output
Compiled by : Haileyesus Demissie Object Oriented Programming 55
Static and Instance Variables
Static Variables
Static variable in Java is variable which belongs to the class and
initialized only once at the start of the execution.
It is a variable which belongs to the class and not to object(instance ).
A single copy to be shared by all instances of the class.
A static variable can be accessed directly by the class name and
doesn’t need any object