Hey guys! So I'm trying to make a simple program that prints out the attributes of "Person". I'm kinda still practicing with java so I'm not sure what the errors are about. Here's what I have:
Driver:

public class Driver {


    public static void main(String[] args) {
      Person a = new Person ("Brian Scalabrine", 22, "male", "b.scalabnrine@gmail.com");
      Person b = new Person ("Anderson Silva", 34, "male", "silvadaspida@gmail.com");



    }

    public static void printSelf(){
        System.out.println("Name: " +name);
        System.out.println("Age: " +age);
        System.out.println("Gender: " +gender);
        System.out.println("Email: " +email);
    }
}

Person

public class Person {
    private String name; 
    private int age;
    private String gender;
    private String email;

    public Person (String name, int age, String gender, String email){ 
        this.name = name; 
        this.age = age;
        this.gender = gender;
        this.email = email; 
    }

    public String getName(){
        return name;
    }

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

        public int getAge(){
        return age;
    }

    public void setAge(int age){ 
        this.age = age; 
    }

        public String getGender(){
        return gender;
    }

    public void setGender(String gender){ 
        this.gender = gender; 
    }

        public String getEmail(){
        return email;
    }

    public void setEmail(String email){ 
        this.email = email; 
    }

}

Recommended Answers

All 5 Replies

"what the errors are about" ...

which errors do you mean?

public static void printSelf(){
        System.out.println("Name: " +name);
        System.out.println("Age: " +age);
        System.out.println("Gender: " +gender);
        System.out.println("Email: " +email);
    }

in the scope of this method, the variables name, age, gender and email are not known. pass a parameter that is an instance of Person, and use it's getter methods.

public static void printSelf(Person p){
        System.out.println("Name: " +p.getName());
        System.out.println("Age: " + p.getAge());
        System.out.println("Gender: " + p.getGender());
        System.out.println("Email: " + p.getEmail());
    }

then call this method from your main method (twice) passing each time one of the two instances of Person you created

cannot find symbol
symbol: variable name
location: class Driver

That is the error I'm getting for this part of my code:

 public static void printSelf(){
        System.out.println("Name: " +name);
        System.out.println("Age: " +age);
        System.out.println("Gender: " +gender);
        System.out.println("Email: " +email);
    }

From the look of it that printSelf method belongs in the Person class, not the Driver, and it should be an instance method, not static. That's because it's obviously intended to print the details of a current Preson instance.

commented: indeed a better solution +13

So what should I do?

as James said: move that method into your Person class, remove the static keyword, and call it as an instance method:

public static void main(String[] args) {
      Person a = new Person ("Brian Scalabrine", 22, "male", "b.scalabnrine@gmail.com");
      a.printSelf();
      Person b = new Person ("Anderson Silva", 34, "male", "silvadaspida@gmail.com");
      b.printSelf();
      }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.