Professional Documents
Culture Documents
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
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);
}
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
}
}
}
THANK YOU