Skip to main content

Java Constructor

Java Constructor is a special method which can be invoked at the time of Object Creation. A constructor is used to initialize the newly created object.
For Example, We have a class called Hello when  you create the object of the Hello Class
Hello Class Constructor gets Invoked  like below
package com.practice;

public class Hello {
 Hello() {
  /* Constructor */
  System.out.println("Hello Class Constructor");
 }

 public static void main(String[] args) {
  Hello hello = new Hello();//Object Creation

 }
}

output
Hello Class Constructor
Rules to declare a constructor
  •  A constructor must have the same name as Class.
  •  A constructor has no return type explicitly.
  •  A constrcutor can contain all Access Modifiers(private,default,protected,public).
  •  A constructor cannot contain Non-Access Modifiers(abstract,final,static,synchronized).
  •  A constructor can have parameters(arguments).
  •  A constructor can have throw clause i.e. constructor can throw an Exception.
 Types of constructors  in Java
  1. default Constructor (or) no-arg Constructor
  2. Parameterized Constructor
Default constructor (or) no-arg Constructor
If the constructor doesn't contain any arguments(parameters) then such type of constructor is known as default constructor (or) no-arg Constructor. In any java class if we don't create any constructor manually compiler creates a one default constructor. If the programmer defines any constructor in java class java compiler will not create any default(no argument) Constructor. The default constructor gets invoked during The Object creation.

Example
public class DefaultConstructorDemo {
//default constructor declaration can be invoked during object creation
public DefaultConstructorDemo() {
  System.out.println("default constructor....");
 }
 public static void main(String[] args) {
  DefaultConstructorDemo d = new DefaultConstructorDemo();
 }
}
output

Parameterized Constructor
   A  Constructor with parameters(arguments)  is called parameterized Constructor. A parameterized constructor is used to initialize the object. initializing the object means assigning values to object. This constructor is user-defined Constructor. Have a look at below example
Example
public class Account {
 private int accountId;
 private String name;
/*parameterized constructor declaration used to
initialize the instance variables.*/
 public Account(int accountId, String name) {
  this.accountId = accountId;
  this.name = name;
 }
 public void printValues() {
  System.out.println("Account id is :"+accountId);
  System.out.println("name is :"+name);
 }
 public static void main(String[] args) {
  Account account = new Account(1, "bala");
  account.printValues();
 }
}

output

Popular posts from this blog

Java Primitive DataTypes

Java data types will tell us what type of data can variable hold. Ex: int x = 10; In the above variable, x can hold int data type value.so we must declare any variable with data type before we can use a variable.

Instance Variables in Java

Instance variables should declare with in the class but outside of any method or block or constructor.instance variables can accessible inside class all methods , blocks & constructors.

Angular 9 Date Pipe Example

Table of Contents what is pipe in angular? Angular Date Pipe Pre-defined Date Format Custom Date Formats what is pipe in angular? In Angular Pipes are used to transform data.Pipes will take input data and transforms input data in desired output. Symbol of Pipe : " | " Angular Date Pipe Angular DatePipe is one of built-in Pipes.DatePipe is used for Format a date value according to locale rules. Syntax {{value_expression| date [:format[:timezone[:locale]]]}} DatePipe avalibale in '@angular/common' module. Example //app.component.ts import   {   Component   }   from   '@angular/core' ; @ Component ({   selector:   'app-root',    template:   `{{dOB | date:'fullDate'}}`,    styleUrls:  ['./app.component.css'] }) expo...