Saturday 23 March 2013

Array of Objects


To instantiate more than one object for a single class, we use Array of Objects.

Example:

import javax.swing.JOptionPane;
class a
{
            int x,y;
            void readdata()
            {
            x=Integer.parseInt(JOptionPane.showInputDialog("Enter x value"));
            y=Integer.parseInt(JOptionPane.showInputDialog("Enter y value"));
            }
            void printdata()
            {
            System.out.println("x="+x+"\n"+"y="+y);
            }
}
class aob
{
            public static void main(String[] args)
            {
            a ob[]=new a[5];
            for(int i=0;i<5;i++)
            {
            ob[i]=new a();
            ob[i].readdata();
            ob[i].printdata();
            }
            }
}

No comments:

Post a Comment