Question

I'm trying to write a very simple, little java program but I'm already stuck at setting the object name. I have 2 classes, first is Starter:

public class Starter {
    public static void main(String args[]) {
        Family tester = new Family();
        tester.setName(testers);        
    }
}

If i'm right I create a Family object called tester, then I use the setName method for giving the family a name. The Family class lookes like this:

public class Family{
    String Name;

    public void setName(String name){
        Name = name;
    }
}

But in the starter class at the tester.setName I get this error: tester cannot be resolved to a variable.

Thanks in advance for the answers!

Was it helpful?

Solution

Replace

tester.setName(testers);

with

tester.setName("testers");

As your Family class's setName() method takes a String object and String needs to be created either as above example or as below example:

String testers = new String("testers");
//and then you can use above String object as in your code snippet (as follows)
tester.setName(testers);

OTHER TIPS

Unlike some other programming languages, in Java Strings must be enclosed in double quotes.

Single quotes are used for chars, a primitive data type.

You can change your code within;

tester.setName("testers");

You have some mistake in the setName():

public class Starter {
    public static void main(String args[]) {
        Family tester = new Family();
//      tester.setName(testers);
//      variable testers is not defined, you means set the name to "testers"? Try this:
        tester.setName("testers");
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top