My program has an AddressBook and Persons class. The AddressBook class has an array of Persons objects.
import java.util.*; public class Persons { private String name; private String lastName; private String addres; private String city; private String state; private int zip; private String phoneNumber; public Persons(String name , String lastname,String phoneNumber) { this.name = name; this.lastName = lastname; this.phoneNumber = phoneNumber; }
and this is the AddressBook.addPerson method:
public void addPerson(Persons prs) { for (int i = 0; i < ArrayOfPerson.length; i++) { if (ArrayOfPerson[i] == null) { ArrayOfPerson[i] = prs; break; } } }
and Main class:
public class Main { public static void main(String[] args) { AddressBook addr = new AddressBook(); addr.addPerson(new Persons("first name", "first lastname", "123"));// in here for example/ how can i use this person object later addr.addPerson(new Persons("second name", "second lastname", "456")); addr.addPerson(new Persons("third name", "thirs last name", "789")); addr.addPerson(new Persons("fourth name", "fourth last name", "101112")); } }
My question is about the Main class. How can I reuse the created object? For example, I would like to do something like this:
System.out.println(x.getname());
where x is the person object created by the new keyword as an argument.
Sorry if my question is a newbie question ... I searched on google and found nothing.
source share