We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › Little class question
Page Index Toggle Pages: 1
Little class question (Read 1369 times)
Little class question
May 18th, 2009, 7:06pm
 
Hello, I'm currently porting a processing sketch into Eclipse, but I've run into some problems concerning accessing variables from other classes.
In my sketch, a global variable is declared before the setup method e.g.
int var1=123;
void setup(){
...
This variable can then be accessed by other classes by its name alone e.g.
class blah{
blahvar=var1;
}
Now in Eclipse, to do the same thing, do I need to declare the variable 'static' for it to accessable in other classes without having to refer to the object that contains it (whatever that is for the main class)?
Thanks,
Rob
Re: Little class question
Reply #1 - May 18th, 2009, 10:43pm
 
Should be able to just make it public rather than static.
Re: Little class question
Reply #2 - May 19th, 2009, 1:00am
 
The answer depends on the structure of your code: will the other classes be internal to the main class, like they are if they live in a .pde file? Will they be independent, as if they live in a .java file?
In the first case, NoahBuddy's answer applies, no prefix needed.
In the second case, you have to use an accessor (or make public) and in all cases you have to specify the class holding the variable, like you have to have a reference to PApplet to call Processing functions.
Re: Little class question
Reply #3 - May 20th, 2009, 5:18pm
 
The other classes are independent. In my case the main class makes an instance of another class which lies in a seperate .java file. How can I get this new instance to be able to access variables of the main class? Do I need to pass the current instance of the main class to the new instance in its constructor? If so, do I just pass it a 'this' as a parameter? Because i've done that so that the independent class can access the processing functions, but it still can't access the variables of the main class. Also what is an accessor?
Cheers,
Rob
Re: Little class question
Reply #4 - May 21st, 2009, 12:20am
 
You are on the right track. Passing 'this' will give a reference to your sketch.

For your other class to access a variable, either the variable needs to be public (i.e. "public int var = 4;") or as PhiLho suggested, use an accessor.

An accessor is a public method that gives regulated access to a private variable. This way, the variable cannot be changed without you knowing it.
Code:
class mysketch extends PApplet {
 int var = 4;//private variable
 ...
 public int getVar() {//an accessor to read var
   return var;
 }

 public int setVar(int newValue) {//an accessor to set var
   var = newValue;
   return var;//to verify change
 }
 ...


Page Index Toggle Pages: 1