You need to sign in to do that
Don't have an account?

Help with apex custom controller and apex class
Hello all,
I have created a visualforce page with a custom controller calls "Xcontroller".
on my controller I have defined a member of my class "Y" like this:
"private static Y y = new Y(a,b);"
This line is executed on page load.
When I click on my page button "finish" I invoke the function I created in my controller called "finish".
In this function I call functions from my class y, for example:
public class y {
private static string z;
private static string A;
private static string B;
public y(string a, string b)
{
A=a;
B=b;
}
public string getZ()
{
return z;
}
public static void setZ(string zz)
{
z=zz;
}
}
on my controller :
public with sharing class Xcontroller{
private static Y y = new Y(a,b);
public Xcontroller()
{
}
public pagereference finish()
{
string w= y.getZ();
}
the problem is that when I click on "finish" the value in 'w' is null although on the page load before it got a value!
Can anyone help me understand what I did wrong?
Thanks a lot,
Inbal
Well, I don't see where you actually call setZ. If you don't, z will always be null.
Other than that, I'm not sure why you are declaring y in your controller as static.
From the docs:
Static variables aren't transmitted as part of the view state for a Visualforce page.
This would cause it not to retain its value. Try removing static from that variable declaration.
Another thing to bear in mind, though I don't think it's directly related to your question, there is a subtle bug in your code. Apex is not case sensitive. So your constructor doesn't actually assign anything to the instance variables A and B. You need to do:
Rich
You hve made right concept,outside scope static variable does not retain it's value.
Actually, using 'this' won't work in this case due to the static modifier on the a and b variables. The original code won't compile either due to duplicate variable declaration. To fix the original, modify either the constructor variable names or the class-level variable names. If you don't need your variables static, remove the static keyword and use 'this'.
I assumed that the code posted was just pseudo-code since it won't even compile as written. The question for the OP is did you really want all those static variables and methods.
Yeah, I totally missed the fact that they're static.
Thank you all,
first you are right it is a pseudo code,
I am calling setZ, at the controller constructor.
also in my code the variables names are not the same, so the problem is not in the names.
sorry i got you confused.
I made all these variables as static since I want to keep their value- any other suggestions on how I will still get the value of w?
Also, let me fix a bit the controller constructor:
public with sharing class Xcontroller{
private static Y y = new Y(a,b);
public Xcontroller()
{
if (y==null)
y= new Y(a,b)
if(w==null)
setZ();
}
public pagereference finish()
{
string w= y.getZ();
}
well small change:
public with sharing class Xcontroller{
private static Y y = new Y(a,b);
public Xcontroller()
{
if (y==null)
y= new Y(a,b)
if(w==null)
setZX();
}
public static void setZX()
{
y.setZ();
}
public pagereference finish()
{
string w= y.getZ();
}
As suggested in my first post, the y variable in your controller class will retain its value on the same page if it is not marked as static. Have you tried that?
If that doesn't work, can you post your actual code? You are checking variable w in your constructor, but it is not declared anywhere except locally in the finish() method. I don't see how this version works either. It's hard to tell you what's wrong when we only have half of your code.
Hi
Removing the static did help...
Thanks everyone,
Inbal