0

I am trying to find a solution to initialize a subclass in a constructor, but I have the following error message : Variable does not exit: Info

public class Products {
    public String Name;
    public class Info{
        public String price;
        public String testField;
    }
    public Products(Products.Info myInfo){
        this.Info = myInfo;
    }
}
1

The inner class is contained in the outer class, but it is not a member of the outer class (that is, it occupies no storage space). To have one or more info members, you need to define them:

public class Products {
    public String Name;
    public Info info; // an instance member named info of type Info
    public class Info{
        public String price;
        public String testField;
    }
    public Products(Products.Info myInfo){
        this.Info = myInfo;
    }
}

Note that there's nothing special about the member name, you can call it whatever you want:

public class Products {
    public String Name;
    public Info productInfo; // an instance member named productInfo of type Info
    public class Info{
        public String price;
        public String testField;
    }
    public Products(Products.Info myInfo){
        this.productInfo = myInfo;
    }
}
1
  • thank you very much ! – Vneq Sep 25 '20 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.