Java Programing laungage

Core Java Tutorial

Introduction of Core Java

How To Install JDk and Set of Path

Syntax of java Program

Difference between Java and C/C++

Advantage and Disadvantage of Java

What is Java

Why Java is not Pure Object Oriented Language

Java has Following Features/Characteristics

Limitation of Java Language and Java Internet

Common Misconception about Java

Simple Program of Java

Integrated Development Environment in java

Compile and Run Java Program

Applet and Comments in Java

Tokens in Java

Keywords in Java

Identifier and Variables in Java


Data Type in Java

Assignments and Initialization in Java

Operators in Java

Rule of Precedence in Java

Operator on Integer and Separators in Java Programming

Java Control Flow of Statements

If and If-else Selection Statement

Nested If-else and If-else-If Selection Statement

switch case and conditional operator Selection Statement

for and while Loop

do..while and for each Loop

break and labeled break statement

continue and labeled continue statement

return Statement and exit() Method

Escape Sequence for Special Characters and Unicode Code

Constants and Block or Scope

Statement in Java

Conversions between Numeric Types in Java

Import Statement in Java

User Input in Java using Scanner Class

User Input in Java using Console Class

Array in Java

One Dimensional Array

Two Dimensional Array

Two Dimensional Array Program

Command Line Argument in Java

String args Types in Java

Uneven/Jagged array in java

Math Class Function and Constant

Math Class all Function used in a program

Enumerated Types in Java

Object Oriented Programming v/s Procedural Programming

Object Oriented Programming Concepts in Java

Introduction to Class,Object and Method in Java

Class Declaration in Java

Class & Objects in java

Encapsulation in Java

Modifiers/Visibility for a Class or Interrface or member of a Class

Polymorphism in Java

Runtime polymorphism (dynamic binding or method overriding)

Constructor in Java
Previous Home Next
  1. Constructor is a member method.
  2. It is called automatically when we create an object.
  3. Its name is same as class name.
  4. It should be defined in public block because called outside the class but it can defined private if called inside the class.
  5. A class can contain more than one constructor if there argument are different and this is called as constructor overloading.
  6. Every class can contain a default zero argument constructor if no explicit constructor is defined by us.
  7. In c++ constructor also provides memory to class member variables inside the object but in java it is the new operator which provide memory to class member variables inside the objects and new also initialize the member variable.
  8. Constructor is not void it implicitly return object of type itself, We can't explicitly return any value from the constructor.
  9. Constructor’s syntax does not include a return type, since constructors never return a value.
  10. New is a keyword as well as operator in java.
  11. If you declare a class with no constructors, the compiler will automatically create a default constructor for the class. A default constructor takes no parameters and has an empty body. Because the compiler will automatically generate a default constructor if you don't declare any constructors explicitly, all classes have at least one constructor.

Example :

Types of java constructors

There are two types of constructors:

  1. Default constructor (no-arg constructor)
  2. Parameterized constructor
  1. Java Default Constructor

  2. Every class has a default constructor (if no explicit constructor) that does not take any argument and its body does not have any statements. The compiler generate the default constructor automatically. The compiler stops genereting default constructor as soon as we add our own constructor. When we do not create any constructor in the class the jvm will create the default constructor and initialize the instance variable with default values null or zero.

    A constructor that have no parameter is known as default constructor.

    Default constructor provides the default values to the object like 0, null etc. depending on the type.

    Syntax :


    Example :

    class A
    	private int x;
    	private int y;
    	void display()
    class Test
    	public static void main(String args[])
    		A a1=new A();

    output :

    0		0

    Note :Here we have not created any constructor then jvm create automatically default constructor after creating object and initialize the instance variable from 0 or null.

    Parameterized constructor

    A constructor that have parameters is known as parameterized constructor.

    Parameterized constructor is used to provide different values to the distinct objects.

    Example :

    class A
                private int x,y;
                A()//default zero argument constructor
                A(int x1)//parametrized one argument constructor
                A(int x1,int y1)
                void display()
        class ATest
                    public static void main(String args[])
                                A a1;
                                a1=new A();
                                A a2;
                                a2=new A(5);
                                A a3;
                                a3=new A(5,6);

    output :

    5,5 5,5 5,6
Previous Home Next