Super friendly <br/>code & developer communities

Super friendly
code & developer communities

A place for coders and developers to share, learn and grow together.

Java Programming | Introduction to constructors

This is a tutorial for introduction to constructors. The program is given below that uses constructor to create a object. The program is extendable. Go enjoy the program. Lets begin…

Before you begin, Do this
Create a separate java file for other class.
Create a java file for main class.
Import the created java class in main class file.
Program for constructors in java.

1. Create a class with constructor

//declare the package name if the class file is
package Myclasses;
//decalre class
public class Number {
    private int a,b,sum;
    //a constructor
    public Number(int x,int y)
    public void addthem()
        sum = a+b;
    public void print()
        System.out.println("Sum = "+sum);


Enter 2 numbers:
Sum = 3

How it Works
1. Objects of class are created by calling constructor.
2. The sum function is called from main class.
3. The print function is called from main class.

Extending it
The program can be extended by using the concept in any program.
Remember this is a basic part of java which can be used in any program.


Your class
1. Create class.
2. Import the Scanner.
3. Declare the class as public
4. Declare variables.
5. Create a constructor.
6. Create functions.

Main class
1. Create other class
2. Import your class.
3. Declare the class as public.
4. Add the void main function.
5. Take inputs.
6. Declare object and call constructor.
7. Call functions.

At the end.
You learnt creating the Java program for Introduction to constructors . So now enjoy the program.

P.S. If you have any questions, click here to join our community and feel free to ask any questions.