Constructor
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
STATIC MEMBERS
The members inside class when declared as a static are called static members .
1. Static variable
2. Static method
3. Static Block
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 .
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);
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.
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);
}
}
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.
class simple
{
public static void main (String args[])
{
Int a=10;
Integer b=new Integer(a);
System.out.println(a);
System.out.println(b);
}
}
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();
}
}