fork download
  1. class Account{
  2. int a;
  3. int b;
  4. public void setData(int c,int d){
  5. a=c;
  6. b=d;
  7. }
  8. public void showData()throws Exception{
  9. System.out.println("Value of a ="+a);
  10. System.out.println("Value of b ="+b);
  11. }
  12. }
  13.  
  14. class ArrayOfObjects{
  15. public static void main(String args[]) throws Exception{
  16. Account[] obj = new Account[4] ;
  17. for (int i=0;i<obj.length;i++)
  18. obj[i] = new Account();
  19. obj[0].setData(1,2);
  20. obj[1].setData(3,4);
  21. System.out.println("For Array Element 0");
  22. obj[0].showData();
  23. System.out.println("For Array Element 1");
  24. obj[1].showData();
  25. }
  26. }
  27.  
Success #stdin #stdout 0.09s 212416KB
stdin
Standard input is empty
stdout
For Array Element 0
Value of a =1
Value of b =2
For Array Element 1
Value of a =3
Value of b =4