java help
•
2 Jan 2009, 15:17
•
Journals
hello fellows!
need some simple (atleast i think so) help with java.
i am initializing an integer.
a method using this integer changes is within this method, but in the class the integer-value is still the old value!
what can i do to change the integer-value to the value it has after the method is finished?
i want to use the updated integer-value with another method!
hope i made it understandable :D
cheers
need some simple (atleast i think so) help with java.
i am initializing an integer.
a method using this integer changes is within this method, but in the class the integer-value is still the old value!
what can i do to change the integer-value to the value it has after the method is finished?
i want to use the updated integer-value with another method!
hope i made it understandable :D
cheers
well, i am initializing an integer
int p = 1;
after this i am using this integer p with some method.
this method contains a while-loop which changes the value p to 21.
so the next method should use this new value p=21.
but i doesnt, it also uses the old value p=1.
in the very beginning its 1
after the while loop its 21
and at the beginning of the second method its 1 again :x
i just started to learn java, so i dont really know much about all this static whatever stuff :P
but if i dont make it static, its not workin at all!
tell me why this one doesnt work:
i made a new method getMax using the same for/while-loops like drawTreetop, but without the printlns.
and i told the method to return the new value for p.
and after drawTreetop i say
p = getMax(blabla);
why isnt it workin? :D
Getters usually look like this:
public TYPE(string, int , or the name of the class of which the object is: ex. Contact) getNAME()
{
return variable;
}
now if you call this getter on a parameter p, you could do it like this
p.getNAME()
and it would return the value
I don't quite get what you are doing there...
my idea...if i define a methode returning an integer, i should be possible to decline a variable with this returned value, or am i mistaken?
some extracs which are neede:
int p = 1;
public static void drawTreetop(int z, int i, int p){
for(int a = 1 ; a < z ; a++){
for(int b = 0 ; b < 3 ; b++){
String output = "";
output += drawSpace(i);
output += drawStar(p);
System.out.println(output);
i = i - 2;
p = p + 4;
}
i = i + 4;
p = p - 8;
}
}
so after thise while/for-loops p got another value.
21 for example.
and after drawTreetop there is another method using p aswell.
but it still uses the old value, eventhough it need to use the new one.
this.p = p;
in drawtreetop would change the value of the p in the class / object to the value of the parameter p. At the moment you are only working on the parameter, that's why your p in the class / object doesn't change.
i saw this sometime ago, but never got where to use it.
so i set p in the class by saying
this.p = p;
within the method?
which p is the one i generated inside of the method?
"this.p" or "p"?
thats the thing i dont get :x
int p1 = 1;
drawTreetop(int z, int i, int p2)
{ .. }
at the end of the method p2 is basically lost and p1 == 1 because that's how you set it
is just an example on how you can use it. If you only write p in drawtreetop java assumes you mean the parameter p (makes sense if you think about it ;-) ). So if you want to change the p of your class / object, you have to use this.p. this. simply refers back to from where a method was called.
in drawtreetop:
this.p -> the p from where the method was called (in your class / object)
p -> the parameter p
this.p = p ;
after the loops are done?
If you solve the static membership issue, you can remove the p parameter from your method declaration.
and i told the method to return the new value for p.
and after drawTreetop i say
p = getMax(blabla);
why isnt it workin? :D
I mean you didn't declare p as a variable but u just use it in functions like u said public bla bla bla (INT P)
Problem is if u do that again with an other funtion (int p) it will just think p is new variable being used in this function...
I'm not sure but just a wild guess ;)
Have you tried setting it as a variable in the class? or are you just using it as a local variable for that function?:d
welll there is something wrong but try working with this.
public static void drawTreetop(int z, int i, int p){
you can just do
public static void drawTreetop(int z, int i){
and use the
private int p
for everything
but if i just say drawTreetop(int z, int i){
my eclipse is crying about p not beein declared :x
{
private int p;
}
public static void drawTreetop(int z, int i){
p = 1;
for(int a = 1 ; a < z ; a++){
for(int b = 0 ; b < 3 ; b++){
String output = "";
output += drawSpace(i);
output += drawStar(p);
System.out.println(output);
i = i - 2;
p = p + 4;
}
i = i + 4;
p = p - 8;
}
static int p = 1;
Basically everything in a static context exist before an object of the class is initiated and it is shared between all instances that you create of the class. You can refer to static variables and functions by Class.variable and Class.function.
All non static functions and variables is not available until you create an object of the class.
Until you learn the basics, I would recommend you to stay as much as you can away from the static scope as possible. Make the functions and variables non static and do something like this:
public static void main(){
Object x = new Object();
x.run();
}
But yea you're right ;)
i.e.
int x
x = method(<arguement>)
?
Also have you declared the orginal integer as a public object?
(check my post up if you want some more details)
int x = 0;
x = methodB(x);
System.out.orintln(x);
}
public int methodB(int x) {
// do something
return (x);
}
Is p a global variable or a local one? and why is your method declared as static? you could set the global p as private static int p; and than use this.p = p; in your method to change its value.
gl solving ur prob
then you have the following two methods:
an accessor - this simply returns the integer value
public int GetMyInt() { return m_integer; }
a mutator - this updates the member integer
public void ChangeMyInt(int value) { m_integer = value; }