0

I working with an java object, in which when I hover over it is defined as follows:

Employee= {Welder@4562}"Welder tech"
  Name = "Fred"
  age = "32"

I am baffled as to what {Welder@4562}"Welder tech" is. However, I need to change "Welder Tech" to some other text. How can I do this? (I believe this object was created by reflections)

1 Answers1

1

Here's a quick explanation that explains why you're getting

{Welder@4562}"Welder tech"

This is due to the default toString() method in Java. In order to change that to provide to match your expected output, you should override it, here's a quick example:

public String toString(){
   return "Hello, I am " + name;
}

Also to change the values of the fields in your objects you should create a getter and a setter for each field as following:

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

If you're unfamiliar with the "this" keyword then please refer to this.

mrhallak
  • 1,138
  • 1
  • 12
  • 27
  • im not able to do this, because the Employee object is already created, then I need to change it's name. I think what I am asking is to change the simpleName of an object. – elutions elutions1 May 30 '17 at 18:07
  • @elutionselutions1 Even if its already created you can still add the setter method or any of the above to the object and it won't affect the existing instances. – mrhallak May 30 '17 at 18:12
  • yes, actualy what i submited to you was wrong - the object does have setters and getters – elutions elutions1 May 30 '17 at 18:14
  • Then all you have to do is call the setters to change the values of your fields and override the toString method to return the string (string representation of the object) that you desire. – mrhallak May 30 '17 at 18:18