Class-in-Java .

In this session i have discussed class in Java .


A Class is a collection of data members and member functions . A class is a blue print .

Important Point -

1-create the class .

2-create the object of class .

3-call the function with dot operator using object .

Class Syntax :

    class Employee{
       
            int empid; //data member
            string empname;//data member
        
            public void display();//memeber function
    }
    

Example : Write a java program that demonstrate the use of class with simple example .

class Employee{
    
        int emp_id;
        string emp_name;
    
        void getdata(int id,int name){
       
            emp_id=id
            emp_name=name;
        }
        void putdata(){
            
            System.out.println("Employee ID : "+emp_id);
            System.out.println("Employee Name : "+emp_name);
        }
        public static void main(String args[]){
       
            Employee emp=new Employee();  //object created
            emp.getdata(1,"Rakesh");     //getdata method called
            emp.putdata();              //putdata method called
        }
}
    
OUTPUT : Employee ID : 1
         Employee Name : Rakesh

Example : Write a java program that demonstrate the use of class .

    class Operation
    {
        int x=20, y=10, result = 0;
        
        public void sum(){
        
            result=x+y;
            System.out.println("Sum : "+result);
        }
        public void sub(){
        
            result = x - y;
            System.out.println("Sub : "+result);
        }
        public static void main(String args[]){
       
            Operation obj=new Employee();  //object created
            obj.sum();     //sum method called
            obj.sub();    //sub method called
        }
    } 
OUTPUT : Sum : 30
         Sub : 10 

Example : Write a program in which use a method to reverse the given number using class .

    class Operation
    {
        int rem=0, rev=0;
        public :
        void reverse(int n){
        
            while(n>0){
            
                rem = n % 10;
                rev = (rev * 10 )+ rem;
                n = n / 10;
            }
            System.out.println("Reverse Number is "+rev);
        }
        public static void main(String args[]){

          Operation obj=new Operation();//object created
          obj.reverse(123);                //reverse method called 
        }
    }
OUTPUT : Reverse Number is : 321

Example : Write a java program in which use a method to sum the individual digit of the given number using class .

    class Operation{
    
        int rem=0, s=0;
        public :
        void Sum(int n){
         
            while(n>0){
            
                rem = n % 10;
                s = s+ rem;
                n = n / 10;
            }
            System.out.println("Sum Number is "+s);
        }
        public static void main(String args[]){

          Operation obj=new Operation();//object created
          obj.sum(123);                //sum method called 
        }
    } 
OUTPUT : Sum : 6
          

Write a java program in which the main method in another class .

    class Operation{
    
        int rem=0, s=0;
        public :
        void Sum(int n){
         
            while(n>0){
            
                rem = n % 10;
                s = s+ rem;
                n = n / 10;
            }
            System.out.println("Sum Number is "+s);
        }
     
    } 
    class Test{
      
        public static void main(String args[]){

           Operation obj=new Operation();//object created
           obj.sum(123);                //sum method called 
        }
    }
    
OUTPUT : Sum : 6

In java main method may be in same class or another class .


Welcome to AndroidATPoint.

back image                     next image