How to use an object that is already created as an argument in java

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; } //some getter and setter here for all private fields and some other code 

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.

+4
source share
4 answers

You just need to assign this object to a variable when you create it, after which you can access it and pass it as a parameter:

 Persons firstOne = new Persons("first name","first lastname","123"); 

And your code will be:

 public static void main(String[] args) { AddressBook addr = new AddressBook(); Persons firstOne = new Persons("first name","first lastname","123"); //You will use it as parameter like this: addr.addPerson(firstOne); 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")); } 

Then you can access it as follows:

 System.out.println(firstOne.getname()); 

Or, without creating a variable, you can get it from the collection at the index addr.get(index) :

 System.out.println(addr.get(0).getname()); 
+4
source

You should store each Persons in a variable like this:

 Persons p1 = new Persons("first name","first lastname","123") 

Then, to get the name field:

 System.out.println(p1.getName()); 
+2
source

You need to point the person to the variable:

 Person x = new Persons("fourth name", "fourth last name","101112")); 

Then you can do:

 addr.addPerson(x); System.out.println(x.getname()); 

or you can take an element from a string:

 System.out.println(addr.get(3).getname()) 
+2
source

You would do something like this:

  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")); Person x = new Persons("third name", "thirs last name","789") addr.addPerson(x); addr.addPerson(new Persons("fourth name", "fourth last name","101112")); System.out.println(x.getName()); } 
+2
source

All Articles