Need of Constructors

As we using set() methods to initialize parameters to objects, but it will be too long when we are creating objects.

Java uses these Constructors to make objects itself initialization means it initializes an object immediately upon creation.

The Constructor name should be same as Class name.

It's definition is same as methods but do not have any return type even void also because it implicitly returns the reference id of an object of class type.

If we not define the constructor in the class the default constructor will automatically initializes its values with default values as

Numeric values - 0/0.0

Boolean - false.

String - null.

If we define a constructor then the default constructor will not in use.

//example for using constructors which initializes the same value for all objects.
class Rectangle
{
double length;
double breadth;
Rectangle() //initializes values for both objects rectangle1 and rectangle2
{
System.out.println("Initializing Rectangle");
length =10.0;
breadth = 12.3;
}
double area()
{
return length*breadth;
}
}

public class RectangleArea
{

public static void main(String []args)
{

Rectangle rectangle1 = new Rectangle(); //constructor for the class is being called.
Rectangle rectangle2 = new Rectangle();

System.out.println("Area of Rectangle1 is::"+rectangle1.area());

System.out.println("Area of Rectangle2 is::"+rectangle2.area());
}
}

Output:

Initializing Rectangle
Initializing Rectangle
Area of Rectangle1 is::123.0
Area of Rectangle2 is::123.0

Parameterized Constructors

As we can see in Rectangle() constructor we can initialize the objects with same values but every objects have their own values.

To initialize different values we have to use these type of constructors.

//example for using parameterized constructor
class Rectangle
{
double length;
double breadth;
Rectangle (double l,double b)
{
  System.out.println("parameterized constructor"); //initializes its values which are passed while creating object
length =l;
breadth = b;
}
double area()
{
return length*breadth;
}
}
public class RectangleArea{

public static void main(String []args)
{

Rectangle rectangle1 = new Rectangle(10.0,3.5); //will call the parameterized constructor
Rectangle rectangle2 = new Rectangle(11.0,4.0);

System.out.println("Area of Rectangle1 is::"+rectangle1.area());

System.out.println("Area of Rectangle2 is::"+rectangle2.area());
}
}

Output:

parameterized constructor
parameterized constructor
Area of Rectangle1 is::35.0
Area of Rectangle2 is::44.0
+

The this Keyword

There will be a situation in methods which will refer to the objects that invoked to it, to allows this java uses this keyword which specifies the current object.

We can use this any where a reference to an object of the current class type.

//example for using this operator
class Rectangle
{
double length;
double breadth;
Rectangle (double l,double b)
{
  System.out.println("Initialization using this operator");
this.length =l;
this.breadth = b;
}
double area()
{
return length*breadth;
}
}
public class RectangleArea{

public static void main(String []args)
{

Rectangle rectangle1 = new Rectangle(10.0,3.5);
Rectangle rectangle2 = new Rectangle(11.0,4.0);

System.out.println("Area of Rectangle1 is::"+rectangle1.area());

System.out.println("Area of Rectangle2 is::"+rectangle2.area());
}
}

Output:

Initialization using this operator
Initialization using this operator
Area of Rectangle1 is::35.0
Area of Rectangle2 is::44.0

Instance variable hiding

In Java we can't use the local variables with the same name as the variables declared in the same block of code.

But we can use the names of the instance variables to local variables by using this keyword which refers to the current object. This is called Instance variable hiding.

//example for Instance variable hiding by this keyword.
class Rectangle
{
double length;
double breadth;
Rectangle (double length,double breadth)
{
  System.out.println("Instance variable hiding");
this.length =length;
this.breadth = breadth;
}
double area()
{
return length*breadth;
}
}
public class RectangleArea{

public static void main(String []args)
{

Rectangle rectangle1 = new Rectangle(10.0,3.5);
Rectangle rectangle2 = new Rectangle(11.0,4.0);

System.out.println("Area of Rectangle1 is::"+rectangle1.area());

System.out.println("Area of Rectangle2 is::"+rectangle2.area());
}
}

Output:

Instance variable hiding
Instance variable hiding
Area of Rectangle1 is::35.0
Area of Rectangle2 is::44.0

Garbage Collection

Java allocates memory to an object by using new operator and it deallocates that memory dynamically this technique is called Garbage collection which in c,c++ done by manually by executing delete operator.

It will deallocates by deleting the memory of objects which don't have any reference to it.

The finalize( ) Method

There will be some situations where some operations to be performed before the object was destroyed, It will be done by finalize() method.

General form

protected void finalize()

{

//body of finalize method

}

protected makes use of finalize() method only.

void is the return type means it will not return.

finalize( ) is only called just prior to garbage collection , It is not called when an object goes out-of-scope.

This means that you cannot know when—or even if—finalize( ) will be executed.

A Stack Class

  • A class is like a “data engine.” No knowledge of what goes on inside the engine is required to use the engine through its controls.
  • As long as we are using the methods the inner data also will changes without effecting the outside of class.
  • A stack stores data using first-in, last-out ordering.
  • Stacks are controlled through two operations traditionally called push and pop.
  • To put an item on top of the stack, you will use push.
  • To take an item off the stack, you will use pop. .
// This class defines an integer stack that can hold 10 values
class Stack 
{
 int stck[] = new int[5];
 int tos;
 // Initialize top-of-stack

Stack() 
{
 tos = -1;
 }

// Push an item onto the stack
 void push(int item) 
{
 if(tos==4)
 System.out.println("Stack is full.");
 else
 stck[++tos] = item;
 }

// Pop an item from the stack
 int pop() 
{
 if(tos < 0) 
{
 System.out.println("Stack underflow.");
 return 0;
 }
 else
 return stck[tos--];
 }
}

public class TestStack 
{
 public static void main(String args[]) 
{
//initializes two stacks.
 Stack mystack1 = new Stack(); 
 Stack mystack2 = new Stack();

 // push some numbers onto the stack
 for(int i=0; i<5; i++) mystack1.push(i);
 for(int i=10; i<15; i++) mystack2.push(i);

 // pop those numbers off the stack
 System.out.println("Stack in mystack1:");
 for(int i=0; i<5; i++)
 System.out.println(mystack1.pop());

 System.out.println("Stack in mystack2:");
 for(int i=0; i<5; i++)
 System.out.println(mystack2.pop());
 }
}

Output:

Stack in mystack1:

4

3

2

1

0

Stack in mystack2:

14

13

12

11

10

results matching ""

    No results matching ""