Batty Batty - 1 year ago 54
Java Question

How to set the "name" of an object when created from a constructor inside a method parameter?

I am creating a new building by doing this.
(I add it to an array at the same time)

Building.buildingList.add(new Building(buildingNameTextField.getText(), phoneNumberTextField.getText(), addressTextField.getText(), emailTextField.getText()));

But i am missing the place where to set a "name" for the new building like in this manner:

Building building1 = new Building();

I tried this: but error is left hand side must be variable.

Building buildingNameTextField.getText() = new Building(....);

So i tried using a local variable:

String building = buildingNameTextField.getText();
Building building = new Building(.....);

But it wants to rename the local variable to building1.

How do Get to set a "name" to a new building when its created when i add it to an array?. Not in the variable sense but that actually object building name. Cause if i need to do getBuildingAdress(). But dont know what do put in for a parameter cause i dont know what name to call for when it got added to the array.

Answer Source

I think you don't exactly understand how object-oriented pragramming language works. What you understands as "name" of building is actualy name of variable of type Building. This variable holds reference to object in memory of type Building.

By calling

Building building1 = new Building();

you create variable of type named "building1" and assigning to it freshly created object of Building type (instance of Building class).

In this code fragment

Building.buildingList.add(new Building(buildingNameTextField.getText(), phoneNumberTextField.getText(), addressTextField.getText(), emailTextField.getText()));

you are adding to list freshly created object of Building that is created with some additional data(using constructor with some parameters). And you are adding that object without assigning it previously to any variable. I'll try to simplify this so you can understand me.. You can think about variable as a box temporary holding your object of Building. Java is language that is compiled so all you see as code is changed by compiler for binary code which can be interpreted by JVM(Envirionment within java works). After compilation all informations like variable names ("building1" in this example) dissapears. So even list don't hold any variable names. That's why you can assign new instance of Building to list without defining any name. Also searching list is impossible by any name of variable for the same reason.

Info about searching list for specific objects is available everywhere so i won't be repeating that. I'll only say that you could search for example by iterating list like that and looking for object you want by it's address.

Building buildingYouAreLookingFor;
for(Building building : Building.buildingList){
    if("SomeCity, Somewhere 12".equals(building.getAddress())) {
        buildingYouAreLookingFor = building;

I assumed that you defined getAddress() getter in your Building class.

And now "buildingYouAreLookingFor" variable holds reference to object you need or null if didn't found it by it's address.

Or you can use Map instead of List if you can easly search by any Strings.

Map<String, Building> buildingsByNamesMap = new HashMap(); //creating Map instance

// adding new building to map
buildingsByNamesMap.put("buildingName1", new Building("Empire State Building","111111111", "New York", ""));
buildingsByNamesMap.put("buildingName2anyString", new Building("Some building","222222222", "Paris", ""));

//searching map for specific Building by assigned String
Building buildingYouAreLookingFor = buildingsByNamesMap.get("buildingName1");

Good luck!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download