(Java) Won't print object attributes in for loop

Liam Ralph :

As something to help me learn Java, I'm trying to make a program with a constructor that creates "simp" (a simp is not code related) objects, that each have three attributes. I make these objects by looping through three for loops simultaneously:

for(int i = 0; i < simpNames.size(); i++) {
      Simp simp = new Simp(simpNames.get(i), simpOrigins.get(i), simpingLevels.get(i));
      System.out.println(simp);
}

and running their attributes through a constructor in another class Simp. I am using ArrayList lists and here is my constructor:

public Simp(String newName, String newOrigin, int newSimpingLevel) {
  name = newName;
  origin = newOrigin;
  simpingLevel = newSimpingLevel;
}

However, whenever I run the function, it outputs "Simp@7cca494b, Simp@7ba4f24f, Simp@3b9a45b3" on three separate lines.

Can anyone tell me why it's doing this and how to fix it?

Komuravelli Sai Charan :

You are trying to print the address of the object when you execute: System.out.println(simp); If you want the attributes of that object you can use

System.out.println(simp.name + “ “ + simp.origin);

If the attributes are public (If they are private you can use their corresponding getters).

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=317845&siteId=1