Simon
Simon

Reputation: 19938

Static reference to a non-static method

I'm getting the following error on Eclipse: Cannot make a static reference to the non-static method setName(String) from the type Person.

I'm trying to create a class called Persons to which I would put in each individual's personal details. Here is an extract of the code:

public class Person {

    private String name;

        public String getName() {
            return name;
        }


        public void setName(String name) {
            this.name = name;
        }

}

Now in the MainActivity.java, I'm creating a method called setDetails to set the Name of a person.

public void setDetails() {
    Person.setName("Simon");
}

Eclipse is suggesting a solution by changing the setName method modifier within the Person class to static. I'm not sure why it needs to be static and can someone please explain to me?

Upvotes: 0

Views: 82

Answers (2)

valentine
valentine

Reputation: 57

Person is a class. Classes are like blueprints or like DNA strands. You can have a blueprint without a building made from it, and you can have DNA without an organism controlled by it. The building and the organism are instances: made from a blueprint. In Java, and many other languages, these instances are called objects.

Some properties of objects are unique to the object itself (a.k.a. non-static). Persons (person objects) have names, but the name varies from object to object. You wrote your getName() function in that way (which is correct). To call a non-static function, you first have to instantiate the object, then call the method on that object. For example:

Person grandma = Person();
grandma.setName('Sally');
System.out.println(grandma.getName());

There are some properties to a Person object that are the same across all objects of the Person blueprint. These are static methods. Static functions cannot access non-static methods unless those methods are associated with an instantiated object. This makes sense because the blueprint cannot know the names of all Person objects created from it. You ask a Person for his or her name, not the blueprint that created all Persons. You access static methods using the class name. For example,

Person.getSpecies();

I am not sure what your MainActivity.java contains, but it sounds like the setDetails() functionality should be done in a constructor, not in a function in your main class. Constructors are what create instantiations of classes or blueprints. You can send in certain properties to a constructor, such as the name of a person. So, I suggest you move your setDetails() function into the Person class. Your new constructor would look something like this:

public Person(String name){
    this.name = name
} 

With the constructor in place, the example I provided above would now look like this:

Person grandma = Person('Sally');
System.out.println(grandma.getName());

That way, you set the person's details at the same time you instantiate the object. This is good programming practice in the world of Object Oriented Programming.

Upvotes: 0

Technivorous
Technivorous

Reputation: 1712

it needs to be static because you're accessing the whole class, and not an instance of it. Either make the variables static, or create an instance of person, then call your method on only that instance. You can do this:

public void setDetails() {
Person person = new Person();
    person.setName("Simon");
}

If you're going to have several names, you'll want an ArrayList....

Upvotes: 2

Related Questions