Java Constructors

Java Constructors


"Use desktop site in Smart phones to visit our blog in best manner"



Before going to java program, if anyone want to execute a Java program in, they will need to install JDK on the system.

How to install JDK on the system

The link given above will help you to know about how to install JDK on your system



1. Program to implement Constructors


import java.io.*;
public class ksm1
{
   int x,y;
   ksm1()
   {
     System.out.println("Constructor Exhibited!!!..");
     x=5;
     y=10;
   }
  public static void main(String[] args)
   {
     ksm1 s=new ksm1();
     System.out.println("The value of X is "+s.x);
     System.out.println("The value of Y is "+s.y);
   }
}



Constructor Exhibited!!!..
The value of X is 5
The value of Y is 10


Explanation :

Class 'ksm1' have variable named x and y. ksm1 is the constructor which initiates the variables x and y. When we are goingg to create object for the class, the constructor 'ksm1' is automatically called and it do its duty.






2. Default Constructor


import java.io.*;
import java.util.Scanner;
public class ksm2fib
{
   int x1,x2,x3;
   ksm2fib()
   {
      System.out.println("Constructor Executed!!!..");
      x1=0;
      x2=1;
   }
   public static void main(String[] args)
   {
      ksm2fib f= new ksm2fib();
      Scanner s = new Scanner(System.in);
      System.out.print("Enter n : ");
      int n=s.nextInt();
      System.out.print("Fibbonacci Series : ");
      for(int i=1;i<=n;i++)
      {
         f.x3=f.x1 + f.x2;
         System.out.println(f.x3);
         f.x1=f.x2;
         f.x2=f.x3;
      }
   }
}



Constructor Executed!!!..
Enter n : 5
1
2
3
5
8


Explanation :

ksm2fib is the class name. When we create object for that class automatically constructor will be invoked. The constructor initiates the variable x1 and x2. When we give value for n, it will do the execution based on the code.






3. Parameterized Constructor


import java.io.*;
public class ksm3squ
{
   int a,c;
   ksm3squ(int x)
   {
      System.out.println("Constructor Executed!!!..");
      a=x*x;
      c=4*x;
   }
   public static void main(String[] args)
   {
      int area=5;
      ksm3squ a1=new ksm3squ(area);
      System.out.println("Area of the Square is "+a1.a);
      System.out.println("Circumference of the Square is "+a1.c);
   }
}



Constructor Executed!!!..
Area of the Square is 25
Circumference of the Square is 20


Explanation :

ksm3squ is class name and it have a constructor with the paramerter 'n'. In this when the object is created for that particular class the parameter will be sent through constructor.






4. Copy Constructor


import java.io.*;
class sum
{
   int a,b;
   sum(int c, int d)
   {
      System.out.print("\nConstructor Executed!!!...\n");
      a = c;
      b = d;
   }
   sum(sum obj)
   {
      System.out.println("\n!!!Copy Constructor Invoked!!!\n");
      a = obj.a;
      b = obj.b;
   }
   int asum()
   {
      return a+b;
   }
}
class ksm4sum
{
   public static void main(String[] args)
   {
      int A=10;
      int B=5;
      sum s = new sum(A,B);
      sum s = new sum(A,B);
      sum s2= new sum(s);
      System.out.println("Sum : "+ s.asum());
      System .out.println("Sum of Copy : "+ s2.asum());
   }
}



Constructor Executed!!!...
!!!Copy Constructor Invoked!!!
Sum : 15
Sum of Copy : 15


Explanation :

This is the example for Copy constructor. The constructor "sum" is invoked when the object is called. According to the code, the execution will be done.



You can also view the outputs for the mentioned above programs, by clicking this link given below

Output Screenshots

I think these examples are useful to create simple java constructor programs.

Subscribe Knowsomethinks blogspot for more news and coding, the notifications of every post will be updated to you. If you have any doubts on this, you can ask me in contact form which is in menu.

Comments

Popular posts from this blog

Simple PHP programs I

Posters about Villages

Year 2020