java help

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
Comments
40
I think I might be able to help you if you could tell me what exactly is wrong XD
:D
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.
Parent
mmm and you made sure it doesn't reset the p after the while? pm spie on irc target
Parent
i checked it with system.out.println.
in the very beginning its 1
after the while loop its 21
and at the beginning of the second method its 1 again :x
Parent
check out the replies.. We are with 4 or more who claim the same so :DDD
Parent
haha, yeah, but i guess i dont know enuff to completely understand what this is all about :D
i just started to learn java, so i dont really know much about all this static whatever stuff :P
Parent
nvm I failed to read that it was static and therefore the this.p is utter bullshit :D
Parent
haha, well, yeah!
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
Parent
well you call a getter..

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...
Parent
what dont you get? all the stuff in general? i have to draw a fuckin christmas tree :D

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?
Parent
paste code here and i will try to help, its not rly clear what u r doing from what u described here (u didnt mention if its static for example)
well, i am not really into java for a long time (actually i would call myself a beginner for sure!)
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.
Parent
You have to use this.! In your code there is no difference between the parameter p in drawtree and the int p in the class. Eg:

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.
Parent
+1 didn't know how to explain but indeed:d
Parent
ha, finally i got to the point where i have to use this.!
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
Parent
basically this.p is just you telling the computer: P is the object i'm working with RIGHT NOW!
Parent
the problem you have is that you are creating int p as a class variable but then passing in a different int p to the method and you aren't returning it so its working something like this

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
Parent
this.p = p;
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
Parent
The "p" paramter hides the p class member :P.Use this.p to access the class member.
Parent
so just say
this.p = p ;
after the loops are done?
Parent
NO ignore what I said :). I didn't notice the function is STATIC. This means there is no "this" associated. You can't access the global p as long it's not himself static or you remove the static modifier from your method.

If you solve the static membership issue, you can remove the p parameter from your method declaration.
Parent
Oh, I didn't think of that. ^^
Parent
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
Parent
well idk if you made that mistake but it's possible you declared the Int p again in the function,

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.
Parent
what the others said, if its in same class you dnt need

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
Parent
hmm, its all in the same class,
but if i just say drawTreetop(int z, int i){

my eclipse is crying about p not beein declared :x
Parent
declare it under public class blabla
{
private int p;

}
Parent
Why didn't anyone notice that there is a static member :P...
Parent
? :D i'm deeply sorry :d!
Parent
set a value for p at start of drawtreetop

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;

}
Parent
It cries because the variable p is not static, declare p in the Class like this:

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();
}
Parent
true but well if he's working with other classes and other methods to create a new one, i highly doubt it's working. He's learning java + doesn't know this. so i doubt he knows how to call methods from other classes like p.getBlabla etc .. XD

But yea you're right ;)
Parent
Cant really make much sense of what your asking, but return the value you want in the method, and set the integer = to that?

i.e.
int x
x = method(<arguement>)

?

Also have you declared the orginal integer as a public object?
hm, alright, gonna try this one. tried it with return statement aswell, but i guess i did it the wrong way around

(check my post up if you want some more details)
Parent
public void methodA() {

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.
nerds united


gl solving ur prob
:D thanx ;)
Parent
well, now its workin! thanx to all of you guys :D
the integer should be declared within the class scope as private
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; }
Back to top