0

I have different packages in my app like this.

enter image description here

CansTypeObject class looks like this

public class CansTypeObject {
    String productId;
    String productName;
    String productPrice;
    String productReturnPrice;
    String productImage;
}

Now, I want to access these strings from MainActivity

CansTypeObject object = new CansTypeObject();

But I cant. If I move the CansTypeObject class to the same package as that of MainActivity, I can access them. What is the solution for this?

3 Answers 3

3

The default scope is package-private. Use the public modifier on the String declaration

public String productId;
Sign up to request clarification or add additional context in comments.

2 Comments

default is package. Not private.
oh.. silly how I missed scope of individual Strings. Thanks
2

The only reason of this behavior is that you declared yours class Strings as package-protected strings. In Java this means, that this fields will be accessible only for classes in the same package.

There are several solutions for your problem. The first (and the worst) - declare your fields as public strings, like this:

public String myField;

The second - create getters for your fields and declare your strings as private fields, like this:

private String myField;

public String getMyField() {
    return myField;
} 

Then, use this getters on your class object.

Hope, it helps.

Comments

0

You can define getter-setter method for the CansTypeObject class as follows for accessing them easily:

public class CansTypeObject {
    String productId;
    String productName;
    String productPrice;
    String productReturnPrice;
    String productImage;

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getProductPrice() {
        return productPrice;
    }

    public void setProductPrice(String productPrice) {
        this.productPrice = productPrice;
    }

    public String getProductReturnPrice() {
        return productReturnPrice;
    }

    public void setProductReturnPrice(String productReturnPrice) {
        this.productReturnPrice = productReturnPrice;
    }

    public String getProductImage() {
        return productImage;
    }

    public void setProductImage(String productImage) {
        this.productImage = productImage;
    }
}

for creating getter-setter method you can use shortcut key for

MAC - command + N

Windows - Alt + Insert

Another way is to open the class for which you have to make getter setter and then right click and from the context menu select Generate option

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.