0% found this document useful (0 votes)
20 views

Constructor

The document discusses constructors in Java classes, explaining that a constructor has the same name as its class and is used to initialize object attributes when an object is created. It provides an example student class with a default constructor and parameterized constructor. The document also covers static class members, the Vector class, and other Java topics like varargs, foreach loops, and the this keyword.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Constructor

The document discusses constructors in Java classes, explaining that a constructor has the same name as its class and is used to initialize object attributes when an object is created. It provides an example student class with a default constructor and parameterized constructor. The document also covers static class members, the Vector class, and other Java topics like varargs, foreach loops, and the this keyword.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Constructor

Constructor is special method of a class whose initialize the object of its class
Rules:
1. Its name must be same as class name
2. Constructor doesn’t have any return type
It invokes or call automatically when object is created
Consider following class :
class student
{
String name;
int rno;
student() // CONSTRUCTOR METHOD
{
name ="ABC";
rno= 123;
}

The above class contains constructor method whose initialize the value of
instance variable by zero when object is created

Program:
class student
{
String name;
int rno;
student() //DEFAULT CONSTRUCTOR
{
name ="ABC";
rno= 123;
}
student(String str, int n) // PARAMETERIZED CONSTRUCTOR
{
name=str;
rno=n;
}
public static void main(String args[])
{
student s1=new student();
student s2=new student("XYZ",456);

System.out.println(s1.name);
System.out.println(s1.rno);
System.out.println(s2.name);
System.out.println(s2.rno);
}
}
OUTPUT:
javac student.java
java student
ABC
123
XYZ
456

CONSTRUCTOR METHOD OVERLOADING :


In java it is possible to create methods that have the same name but different
parameter list and different definition is called method overloading .
When we called method using object java compiler match the following three
things
1 Name of method
2 Number of parameter
3 Types of parameter

Consider following example :


class person1
{
int id;
String name;
int age;
person1(int x,String y)
{
id=x;
name=y;
}
person1(int x, String y,int z)
{
id=x;
name=y;
age=z;
}
void display()
{
System.out.println("id "+id+"\n"+"name "+name+"\n"+"age "+age);
} }
class person
{
public static void main(String args[])
{
person1 p1=new person1(101,"RAJ");
person1 p2=new person1(102,"Rahul",32);
p1.display();
p2.display();
}
}
Output:
javac person.java
java person
id 101
name RAJ
age 0
id 102
name Rahul
age 32

STATIC MEMBERS
The members inside class when declared as a static are called static members .
1. Static variable
2. Static method
3. Static Block

Static variable (Static Field):


A variable declared as static is known as static variable
Static variable is used to provide common value to all object
Only one memory space is created static field and share by all object .
Example :
Static int count;
Static String str;
Static method :
Method declared as static is known as static method
The static method is always belong to the class instead of object
Static method can have access to only other static data .
Static variable and Static method access:
Directly access for single same class but for multiple class it used class name
Example:
Class staticDemo
{
Static int a=10; // Static Variable or Field
Static void display() // Static Method
{
System.out.println(“Static method”);
}
Static // Static block
{
System.out.println(“Static block”);
}
public static void main(String [] args)
{
System.out.println(a);
Display();
} }
For multiple class:
class statict
{
static int a=10; // Static Variable or Field
static void display() // Static Method
{
System.out.println("Static method");
}
static // Static block
{
System.out.println("Static block");
}
}
class staticDemo
{
public static void main(String[] args)
{
statict c=new statict();
System.out.println(c.a);
c.display();
}
}
OUTPUT:
javac staticDemo.java
java staticDemo
Static block
10
Static method

VECTOR CLASS:
In java dynamic array known as vector, vector is class in present in java.util
package.
It is used to store objects of different types and size in any numbers.
Vector are created as follows:
Vector v=new Vector(); // without size
Vector v=new Vector(10); // with size
( int a[]=new int[10];)
Following are the advantages of vector over array
1. Vector can be use to number of object that may vary in size.
2. We can add and delete object from vector when required.
3. Avoid memory wastage

Using vector we can not directly store primitive data types we can only
store object there for we need to convert a simple data to object type .

***DIFFERENTS BETWEEN ARRAY AND VECTOR


METHOD
1.addElement(item);
2.insertElementAt(item,n)
3. size();
4. elementAt(n);
5. removeElement(item);
6. removeAllElements();
7. copyInto(array);
8.add()

PROGRAM:

import java.util.*;

class vectorDemo
{
public static void main(String args[])
{
Vector v=new Vector();
v.addElement("kareena");
v.addElement("sona");
v.addElement("reena");
v.insertElementAt("meehir",1);

System.out.println("add element is"+v);


}
}

Output:
javac vectorDemo.java
java vectorDemo
add element is[kareena,sona,reena,meehir]
VARIABLE ARGUMENTS (varargs):
The varargs allows the method to accept zero or multiple arguments .
Before varargs either we use overloaded method or take an array as
method parameter .
But it was not a good programming ,if we don’t know how many parameter
We will have to pass in the method .
Then varargs is better approach .

Syntax:
The varargs uses ellipsis that is three dots (…) after the data type.

ReturnType methodName (Data type . . .Variable)---------------------> int[]


{

EXAMPLE:

class A
{
void add(int…a)
{
int sum=0;
for (int x:a)
{
sum=sum+x;
}
System.out.println(“Sum =”=sum);
}}
class varDemo
{
public static void main(String args[])
{
A a1=new A();
a1.add();
a1.add(10,20);
a1.add(10,20,30);
a1.add(10,20,30,40);
}
}

FOR EACH LOOP:


For each loop mainly used to fetch the value from a collection like array .
The advantage of for each loop is that it eliminate possibility of error and
make cod simple .
Syntax:
For (datataype variable : array/collection)

class forEach
{
public static void main(String args[])
{
Int a[]={10,20,30,40};
For(int b:a)
{
System.out.println(b+“ ”);
}
}
}
WRAPPER CLASSES :

We know that vector can not handle primitive data type like
int,float,long,char double
Primitive data type may be converted into object type by using wrapper
classes in java.lang package.

Primitive Type Wrapper Classes


boolean Boolean
char Character
char Byte
short Short
int Integer
float Float
double Double

Autoboxing: Converting a primitive type value to object of a wrapper class

class simple
{
public static void main (String args[])
{
Int a=10;
Integer b=new Integer(a);
System.out.println(a);
System.out.println(b);
}
}

Unboxing: Converting an object of a wrapper type into primitive type .

class simple
{
public static void main (String args[])
{
Integer b=new Integer(10);
Int b=a;
System.out.println(a);
System.out.println(b);
}
}

THIS KEYWORD:
There can be lots of usage of this keyword
In java this is reference variable that refers to current object of class.
Usage of this keyword
1. This keyword can be used to refer current class instance variable .
2. This() can be use to invoked current class constructor in another
constructor
3. This keyword can be use to invoked current class method
4. This keyword can be pass as a argument in constructor called
5. This keyword can be pass as a argument in the method called
6. This keyword can also be used to return current class object

class Student
{
Int id;
String name;
Void getdata (int id, String name)
{
this.id=id;
this.name=name;
}
Void putdata()
{
System.out.println(“id=”+id);
System.out.println(“Name=”+name);
}}

Class ThisDemo
{
public static void main (String args[])
{
student s=new student();
s.getdata(1,”abc”);
s.putdata();
}
}

You might also like