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]
# 1

It could be a field of that class

georgemca at 2007-7-29 16:58:59 > top of Java-index,Java Essentials,Java Programming...
# 2

E.g.public class A

{

private int index;

public void someMethod( final String s )

{

index = s != null ? s.length() : -1;

}

}

thomas.behra at 2007-7-29 16:58:59 > top of Java-index,Java Essentials,Java Programming...
# 3

but then can you use the new value of index in another method?

mark07a at 2007-7-29 16:58:59 > top of Java-index,Java Essentials,Java Programming...
# 4

> 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() {

// ...

}

}

java_knighta at 2007-7-29 16:58:59 > top of Java-index,Java Essentials,Java Programming...
# 5

> 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

java_knighta at 2007-7-29 16:58:59 > top of Java-index,Java Essentials,Java Programming...
# 6

> 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?

mark07a at 2007-7-29 16:58:59 > top of Java-index,Java Essentials,Java Programming...
# 7

> > 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?

BigDaddyLoveHandlesa at 2007-7-29 16:58:59 > top of Java-index,Java Essentials,Java Programming...
# 8

> 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;)

Newworld20a at 2007-7-29 16:58:59 > top of Java-index,Java Essentials,Java Programming...
# 9

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

java_knighta at 2007-7-29 16:58:59 > top of Java-index,Java Essentials,Java Programming...
# 10

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

BigDaddyLoveHandlesa at 2007-7-29 16:58:59 > top of Java-index,Java Essentials,Java Programming...
# 11

i figured it out i was just declaring it wrong lol

mark07a at 2007-7-29 16:58:59 > top of Java-index,Java Essentials,Java Programming...
# 12

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

java_knighta at 2007-7-29 16:58:59 > top of Java-index,Java Essentials,Java Programming...
# 13

That's my point. Mark07 was just confused and writing incoherently.

BigDaddyLoveHandlesa at 2007-7-29 16:58:59 > top of Java-index,Java Essentials,Java Programming...
# 14

> 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

mark07a at 2007-7-29 16:58:59 > top of Java-index,Java Essentials,Java Programming...