Casa > H > How To Use An Array Of Objects And Call Them In Methods In Java

How to use an array of objects and call them in methods in Java

  1. class Student 
  2. private String name; 
  3. private int roll; 
  4.  
  5. public void setName(String name) 
  6. this.name=name; 
  7. public void setRoll(int roll) 
  8. this.roll=roll; 
  9. public String getName() 
  10. return name; 
  11. public int getRoll() 
  12. return roll; 
  13. ------------------------------------------------------------- 
  14. import java.io.*; 
  15. class Department 
  16. public static void main(String args[]) 
  17. BufferedReader br=new BufferefReader(new InputStreamReader(System.in)); 
  18. System.out.println("How many elements you want to create in the array?"); 
  19. int n=Integer.parseInt(br.readLine()); 
  20.  
  21. Student students[]=new Student[n]; //Creating array of object of Student class 
  22.  
  23. for(int i=0;i
  24. students[i]=new Student(); //Instantiation of object 
  25. System.out.println("Enter name"); 
  26. String na=br.readLine(); 
  27. System.out.println("Enter roll"); 
  28. int r=Integer.parseInt(br.readLine()); 
  29. students[i].setName(na); 
  30. students[i].setRoll(r); 
  31. Department obj=new Department(); 
  32. obj.displayDetails(students); 
  33.  
  34. public void displayDetails(Student[] s) 
  35. for(Student stu:s) 
  36. System.out.println("Name: "+stu.getName()); 
  37. System.out.println("Roll: "+stu.getRoll()); 

De Urita Overy

Como criar um logo plano :: Qual é a diferença entre ver televisão e streaming?