PNR Class Basics
PNR Class Basics
Defining a Class in
JAVA
Dr. Purvi Ramanuj
Dr Purvi Ramanuj
class Circle{
double r;
public static void main(String[] args){
Circle c2 = new Circle(); // OK, default constructor
Circle c = new Circle(2.0); //error!!
}
}
public MyClass(int y) {
x = y;
}
// Default constructor provides the default values to the object like 0, null
System.out.println(geek1.name);
System.out.println(geek1.num);
}
}
Dr. Purvi Ramanuj
Constructor called
null
0
class Geek
{
// constructor with one argument
Geek(String name)
{
System.out.println("Constructor with one " + "argument - String : " + name);
}
// Constructor with one argument but with different type than previous..
Geek(long id)
{
System.out.println("Constructor with one argument : " + "Long : " + id);
}
}
Used to call
constructor of same
class from another
constructor
class Circle{
double r;
public Circle(){
// r = 1.0; //default radius value;
this (1.0); //call another constructor
}
public Circle (double r) {
this.r = r; //same name!
}
public static void main(String[] args){
Circle c = new Circle(2.0); //OK
Circle c2 = new Circle(); // OK now!
}
}
1
E_Number
2 Give Default Values
Marks
• Finaliseexa.java