0

This is for a school project. I have built a simple class with 3 string variables and a constructor to fill these fields.

public class Names {
String firstName;
String middleName;
String lastName;

public Names(String name){
    System.out.println("Passed name is: " + name);
}

public void setFirstName(String name){
    firstName = name;
}
public void setMiddleName(String name){
    middleName = name;
}
public void setLastName(String name){
    lastName = name;
}

public static void main(String []args){
    Names drew = new Names("Drew");

    drew.setFirstName("Drew");
    drew.setMiddleName("Leland");
    drew.setLastName("Sommer");

    System.out.println(drew.firstName + " " + drew.middleName + " " + drew.lastName);
}

public getFirstName(String name){

}
public getMiddleName(String name){

}
public getLastName(String name){

}}

At the bottom where it is getFirstName, getMiddleName, getLastName I want to be able to pass something like getFirstName(drew) and have it return drew.firstName?

I am very new to java FYI.

Drew
  • 1,171
  • 4
  • 21
  • 36
  • Get the parameters out of your getXxxx() methods. They should be parameterless and should return the requested value. Any tutorial will show you this, in particular [this one](http://docs.oracle.com/javase/tutorial/reallybigindex.html). Also look at [Java Info](http://stackoverflow.com/tags/java/info). – Hovercraft Full Of Eels Jan 18 '15 at 02:09

4 Answers4

1
public String getFirstName() {
    return this.firstName;
}

This will return the firstName of the object you call it on.

You can call it like this:

System.out.println(drew.getFirstName() + " " + drew.middleName + " " + drew.lastName);

You can then do the same thing for getMiddleName and getLastName.

user253751
  • 57,427
  • 7
  • 48
  • 90
1

These are "getter" methods to return the values of instance fields. drew is your current Names instance here, therefore if you call these methods on this instance, you'll receive the values you've set with your "setter" methods. And since you're calling them on a specific instance, you don't need to pass it as a method argument. That is why these getter methods are normally parameterless.

They should look like this:

public String getFirstName() {
    return firstName;
}

public String getMiddleName() {
    return middleName;
}

public String getLastName() {
    return lastName;
}

Please note that I've added the corresponding return type (String), because the data type of each instance field is String.

Your println call in the main method would then look like this:

System.out.println(drew.getFirstName() + " " + drew.getMiddleName() + " " + drew.getLastName());
Community
  • 1
  • 1
Tom
  • 16,842
  • 17
  • 45
  • 54
0

Your get methods will be called by an instance of your Names class. When you create an instance of the class and assign it a variable name, just use that variable name to call the method and it will return the name for that instance.

//Instantiate the Names class
Names drew = new Names("Drew");

//Call methods to set the names
drew.setFirstName("Drew");
drew.setMiddleName("John");
drew.setLastName("Smith");

//Call methods to get the names
drew.getFirstName();   //Returns "Drew"
drew.getMiddleName();  //Returns "John"
drew.getLastName();    //Returns "Smith"

And, like others suggested, your get / set methods should be like this:

public void setFirstName(String n){
    firstName = n;
}
public String getFirstName(){
    return firstName;
}
Stack Underflow
  • 2,363
  • 2
  • 26
  • 51
0

as you said, "I want to be able to pass something like getFirstName(drew) and have it return drew.firstName" so the impl is simple,

public String getFirstName(Names other) {
    return other.firstName;
}
cheyiliu
  • 1
  • 1