Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to access non-static variables across classes  RSS feed

Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I have a midlet class (called ClassesTestMIDlet) and two other classes (Calcs and MoreCalcs).

I would like to know if it possible for the methods in Calcs and MoreCalcs to access variables from the ClassesTestMIDlet class without having to declare the ClassesTestMIDlet variables static, and without having to pass the current instance of ClassesTestMIDlet when constructing the other classes.

Here is the code....

.... which gives the following compile error:
ClassesTest/ [14:26] non-static variable testInt cannot be referenced from a static context
ClassesTestMIDlet.testInt = p;
1 error
Errors compiling MoreCalcs.

So was just wondering if anyone knows whether it's possible?

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!