Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to access non-static variables across classes  RSS feed

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

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/MoreCalcs.java [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?


Thanks,
James
 
Onion rings are vegetable donuts. Taste this tiny ad:
Thoughts on deprecation in Java
https://coderanch.com/t/683016/java/Deprecation-Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!