supid question but..
i just can't think right now I am passing strings to methods in a class and i keep lowering the "index" value by deleting stuff out of them but i use while loops usuing index as the upper bound how can i declare an integer in a class so that i can use it in every method and modify it?
[293 byte] By [
mark07a] at [2007-11-27 11:34:45]

It could be a field of that class
E.g.public class A
{
private int index;
public void someMethod( final String s )
{
index = s != null ? s.length() : -1;
}
}
but then can you use the new value of index in another method?
> i just can't think right now
> I am passing strings to methods in a class
Right...
> and i keep lowering the "index" value
Which index ?!
> by deleting stuff out of them
Deleting which stuff ?
> but i use while loops usuing index as the upper bound
Er... not very clear to me.
> how can i declare an integer in a class so that i can use it in
> every method and modify it?
I really didn't understand anything of what you wrote previously, but if you want to access and reuse a variable in many methods you declare it as a field of your class.
public class MyClass {
private int x;// this variable is declared as a field and is therefore
// accessible within all methods of your class
public void doSomething() {
// ...
}
}
> but then can you use the new value of index in
> another method?
The value of the field variable doesn't change between methods -- it is kept in memory. But, within a method, you can change its value as you like.
Message was edited by: java_knight
> The value of the field variable doesn't change
> between methods -- it is kept in memory. But, within
> a method, you can change its value as you like.
is there a way to change it between methods?
> > The value of the field variable doesn't change
> > between methods -- it is kept in memory. But,
> within
> > a method, you can change its value as you like.
>
>
> is there a way to change it between methods?
Huh? The same way the light in your fridge randomly blinks when the door is closed?
> is there a way to change it between methods?
um, write another method that changes the value in between the two methods that you wanted the variable to be changed to. =P
Someone tell me if they even understood that;)
> is there a way to change it between methods?
Yes -- you simply change its value.
public class MyClass {
private int a = 0;
private void foo() {
a = 42;
}
private int bar() {
return a;
}
}
After a call to bar(), a has a value of 0. If you call foo() and then bar(), the latter returns 42.
Hope you got it.
> > is there a way to change it between methods?
>
> Yes -- you simply change its value.
>
> > public class MyClass {
>
>private int a = 0;
>private void foo() {
>a = 42;
>
>
>private int bar() {
>return a;
> }
> }
>
>
> After a call to bar(), a has a value of 0. If you
> call foo() and then bar(), the latter returns 42.
> Hope you got it.
But that's not changing it between methods,
that's changing it during method foo.
i figured it out i was just declaring it wrong lol
> But that's not changing it between methods,
> that's changing it during method foo.
How could you change a variable value "between methods" (I think you mean "outside methods") and not "during a method" (I think you mean "inside a method")?
Maybe I'm missing something, but I'd like to see that.
Apart from setting a variable value at its initialization, when a object is built or as result of a static initialization, I can't see how you can do that.
That's my point. Mark07 was just confused and writing incoherently.
> That's my point. Mark07 was just confused and writing
> incoherently.
:) pretty much i gota quit asking questions when im asleep cuase i write some pretty stupid stuff