简体   繁体   中英

Using a user entered string as an instance name in Java

I am developing an android app in Java, where I have this contacts class defined.

What I want is that user enters a person name in an EditText and clicks a button and it becomes a instance name of a new instance of contact class.

For eg if user enters harry in EditText then something like this :

ButtonClick {
Contact harry = new Contact();
}

Another button click with entered name as Duke then

ButtonClick{
Contact Duke = new Contact();
}

I am new in this domain.

Understanding names in programming is a deep topic. The Java Language Specification has a 60-page chapter on Names and even that doesn't get to your more fundamental question.

There's a conceptual leap for new programmers to get from how we might think about variables in algebra class to how we need to think about variables when programming a computer.

Writing code like this will not pan out:

Contact vibhum = new Contact(email, phone);
database.save(vibhum);
println("New contact " + vibhum + " successfully created.");

The variable name vibhum is how this code refers to an instance of the Contact class. Changing it requires editing the code and recompiling the code. The variable name has no influence outside this piece of source code: the Contact object does not know its own name, it can't print its name, and saving it to a database can't store or retrieve it by name. The variable name is not even available to the running program (although it might be available to debugging tools).

Instead we need to write code like this:

Contact c = new Contact("Vibhum", email, phone);
database.save(c);
println("New contact " + c + " successfully created.");

Now the Contact instance can remember, print, and save its user-visible name.

When a program deals with multiple Contacts, it should have variable names that relate to how it handles those Contacts, not with the Contact names. Examples:

Contact sender = database.get("myself");

Contact c = database.getContact(name);
c.phone = newPhone;
c.friends.add(sender);
database.save(c);

for (Contact contact: emailList) { ... }

A program might operate on all the Contacts in a list (like emailList , above), or fetch a Contact by name or role from a database or from a list or other collection. In any case the variable name is only how the source code distinguishes that variable from all the other objects it deals with. The variable name has nothing to do with the external name of the Contact.

Consider programming a robot to swap two bolts in a parts tray: pick up the bolt from bin #5 to its right hand; pick up the bolt from bin #15 to its left hand; and put them down into the opposite bins. To the robot program, the variable names are "right" and "left". To the external world, the parts tray has bin numbers and the individual bolts might be distinguished by their lengths.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM