1

I have this code(class in class) in java (andorid)

public class History extends Activity {

public Integer Type;

 private static class HistoryAdapter extends BaseAdapter {

      //I don't know how to access variable "Type" here
     Integer num = Type;// error
       .
       .
       .
     }
      .
      .
      .

}

I can't add more classes above "extends BaseAdapter"

Can someone help me, how can I access variable Type in class HistoryAdapter

Thanks

1
  • 1
    What do you mean you "can't add more classes above extends BaseAdapter? And you cannot access instance fields from another class without an instance of the outer class (History). Commented Oct 28, 2010 at 0:55

2 Answers 2

3

HistoryAdapter is a static class, which means that it doesn't have access to its parent class History. You'll either need to not make it not static (if possible), or you need to have the parent class pass the value of Type in through a helper function.

Example:

public class History extends Activity {
    public Integer Type;

    private static class HistoryAdapter extends BaseAdapter {

        Integer num;

        HistoryAdapter(Integer num) {
            this.num = num;
       }
    }

    void foo() {
        HistoryAdapter = new HistoryAdapter(Type);
    }
}

or

public class History extends Activity {

public Integer Type;

 private class HistoryAdapter extends BaseAdapter {

     Integer num = Type;
       .
       .
       .
     }
      .
      .
      .
}
Sign up to request clarification or add additional context in comments.

Comments

1

If your inner class needs access to the fields of the containing class, it has to not be static... then it will have that access. Alternatively, you can just give HistoryAdapter a constructor that takes an Integer type and pass the type from History to it when you create it.

Also, please don't capitalize the first letter of fields or variables in Java.

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.