Search code examples
javaoopdependency-injectiondomain-driven-designinversion-of-control

Get String text which is wrapped in a object


I want to create objects of a class which hold a specific string value. The string value will be passed through the constructor of the class, like this

public class Class_A {

   private String id;
   
   public Class_A (String id) {
      this.id = id;
   }
}

Class_A is a class in my core package. Now I have Class_B, which is a class in my service package. Class_B holds a function for creating a list of Class_A objects with individual strings.

How I can get the specific string value of each Class_A object when I access the list?


Solution

  • You can provide a get method for the string.

    public class Class_A {
    
       private String id;
       
       public Class_A (String id) {
          this.id = id;
       }
    
       public String getId(){
          return id;
       }
    
       public String toString(){
         return id;
       }
    }