You are on page 1of 9

Object Oriented

Programming with
Java(CT405-N)
Prof. Vimal Bhatt , Assistant
Professor
Computer Science & Engineering
Vidush Somany Institute of Technology & Research,Kadi
Chapter 2
Basics of objects and
classes in java
Constructors in Java

When is a Constructor Called


What is Math class?
 Java constructors or constructors in Java is  Each time an object is created using a new() keyword, at
a terminology been used to construct .
least one constructor (it could be the default constructor) is
something in our programs. A constructor in invoked to assign initial values to the data members of the
Java is a special method that is used to same class.
initialize objects.
 The constructor is called when an object of a The rules for writing constructors are as follows:
class is created. It can be used to set initial
values for object attributes.
•Constructor(s) of a class must have the same name as the class
Types of Constructors in Java name in which it resides.
•A constructor in Java can not be abstract, final, static, or
Synchronized.
•Access modifiers can be used in constructor declaration to
control its access i.e which other class can call the constructor.
Types of Constructors in Java

No-argument constructor Parameterized Constructor

 A constructor that has no parameter is known as  A constructor that has parameters is known as parameterized
the default constructor. If we don’t define a constructor. If we want to. initialize fields of the class with our
constructor in a class, then the compiler creates own values, then use a parameterized constructor..
a default constructor(with no arguments) for
the class. And if we write a constructor with
arguments or no-arguments then the compiler class AddDemo1
{
does not create a default constructor.
AddDemo1()
{
int a=10;
int b=5;
int c;
c=a+b;
System.out.println("*****Default Constructor*****");
System.out.println("Total of 10 + 5 = "+c);
}

public static void main(String args[])


{
AddDemo1 obj=new AddDemo1();
}
}
Types of Constructors in Java

Constructor Overloading
class Cricketer
{
String name;  } .
String team;
int age;  Class test:
Cricketer () //default constructor.  {
{  public static void main (String[] args)
name ="";
 {
 team ="";
 age = 0;  Cricketer c1 = new Cricketer();
 }  Cricketer c2 = new Cricketer("sachin", "India", 32);
Cricketer(String n, String t, int a) //constructor overloaded  Cricketer c3 = new Cricketer(c2 );
{  System.out.println(c2);
name = n;  System.out.println(c3);
 team = t;
 c1.name = "Virat";
 age = a;
 }  c1.team= "India";
 Cricketer (Cricketer ckt) //constructor similar to copy constructor  c1.age = 32;
of c++  System .out. print in (c1);
 {  }
 name = ckt.name;  }
 team = ckt.team;
 age = ckt.age;
 }
 public String toString()
 {
 return "this is " + name + " of "+team;
 }
Constructors with this keywords

class abc
{
public abc()
{
this(5);
System.out.println("Default Constructor"); .
}
public abc(int x)
{
this(5, 6);
System.out.println("Constructor with one Parameter");
System.out.println("Value of x ==> "+x);
}
public abc(int x, int y)
{
System.out.println("Constructor with two Parameter");
System.out.println("Value of x and y ==> "+x+" "+y);
}
}
class ChainingDemo1
{
public static void main(String as[])
{
abcobj = new abc();
}
}
Static Variable in Java
 Static variables defined as a class member can be accessed without public class Test
object of that class. Static variable is initialized once and shared {
among different objects of the class. All the object of the class having static int x = 100;
static variable will have the same instance of static variable.
 Note: Static variable is used to represent common property of a class.
int y = 100;
It saves memory. public void increment()
 Example: {
 Suppose there are 100 employee in a company. All employee have its x++; y++;
unique name and employee id but company name will be same all 100
employee. Here company name is the common property. So if you }
create a class to store employee detail, then declare company_name public static void main( String[] args )
field as static {
 Below we have a simple class with one static variable, see the
example.
Test t1 = new Test();
Test t2 = new Test();
t1.increment();
t2.increment();
System.out.println(t2.y);
System.out.println(Test.x); //accessed without any
instance of class.
}
class ST_Employee
{
int eid; class Test
String name; {
static String company_name;
public static void square(int x)
static { {
company_name ="StudyTonight"; //static block invoked System.out.println(x*x);
before main() method
}
}

public void show() public static void main (String[] arg)


{ {
System.out.println(eid+" "+name+" "+company_name); square(8) //static method square () is called withou
} any instance of class.
public static void main( String[] args ) }
{ }
ST_Employee se1 = new ST_Employee();
se1.eid = 104;
se1.name = "Abhijit";
se1.show();
}

}
THANK YOU

You might also like