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 class

This is a tutorial for introduction to class in java. The program is given below that uses class to add two numbers. 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 using class in java.

1.Create file

//declare the package name if the class file is
//created in other package.
package Myclasses;
//import scanner
import java.util.Scanner;
//declare class
public class add {
    //declare variables.
    private int a,b,sum;
    //functions should be public if needed to access from other class
    public void getdata()
        //print message to enter numbers
        System.out.println("Enter 2 Numbers:");
        //Take input
        Scanner input = new Scanner(;
        a = input.nextInt();
        b = input.nextInt();
    public void sum()
        sum = a+b;
    public void print()
        System.out.println("Sum = "+sum);

2.Create (run ClassIntro file)

//import our class
import Myclasses.add;
// the name of our class its public
public class ClassIntro {
    //void main
        public static void main (String[] args)
            //declare class
            add myclass = new add();
            //call functions


Enter 2 Numbers:
Sum = 4

How it Works
1. First the class is created.
2. The getdata function is called from main class.
3. The sum function is called from main class.
4. The print function is called from main class.

Extending it
The program can be extended by using the concept in any program. Like for ex. you can create a same program for subtraction, multiplication,division,etc. Go ahead extend it.
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 variable.
5. 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. Declare class.
6. Call functions.

At the end.
You learnt creating the Java program for Introduction to class . 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.