Declare variables vs passing parameters

Hi,

I'm just curious about it. What is the difference in declaring an variable (static or not) and having your methods use that (get/set) and declaring a method variable and passing that along other methods that you call?

Some people have said that variables that have to be used by more than 1 method should declare an instance variable.

Can anyone enlighten me?

Thanks.

Desmond

[417 byte] By [sandridera] at [2007-11-27 10:27:30]
# 1

I'm not sure I understand you. Can you illustrate with code examples.

floundera at 2007-7-28 17:46:09 > top of Java-index,Java Essentials,Java Programming...
# 2

Are you asking about the pourpose of inspectors (the get methods)?

You have to understand whats the pourpose of a defined Object by a programmer.

public class Sum {

private int num1 = 0;//this private variables belongs to the Object Sum, and normally they are

private int num2 = 0;//ALWAYS PRIVATE

public Sum(int num1, int num2){//this is the constructor, its where u initialize ur private variables

this.num1 = num1;

this.num2 = num2;

}

public int getNum1() {//this is an inspector, it gets the num1 of the private variable num1

return num1;

}

public int getNum2() {//this is another inspector but this time for num2

return num2;

}

/*

* Whats the pourpose of the inspectors? Why not make the private variables public?

* Heres the answer..If u make those private variables public, you risk on having

* more mistakes in ur program, you always should restrict variables as much as possible

* so the chances on making mistakes decreases

*/

public int result() {//this isnt an inspector, but a method that returns the sum of both num1 and num2

return num1 + num2;

}

/*

* Now how to use this Object to ur needs

*/

public static void main(String[] args){

Sum add = new Sum(12, 10);

System.out.println("Add");

System.out.println("Num1: " + add.getNum1() + "\n" +

"Num2: " + add.getNum2() + "\n" +

"Result: " + add.result());

/*

* You can make more Sum's if u like but each of them have their own variables

*/

Sum another_add = new Sum(20, 15);

System.out.println("Another add");

System.out.println("Num1: " + another_add.getNum1() + "\n" +

"Num2: " + another_add.getNum2() + "\n" +

"Result: " + another_add.result());

/*

* Now that whe have both results we can compare them

*/

System.out.println("Result in add: " + add.result() + "\n" +

"Result in another_add: " + another_add.result());

}

}

HighMagea at 2007-7-28 17:46:09 > top of Java-index,Java Essentials,Java Programming...
# 3

Oh sorry, I think I have confused people.

What I'm asking is this. I can declare an instance variable and use it like this

private String s;

private static String s;

private void doSomething() {

// do whatever to s

}

private void doSomething2() {

// do whatever to s

}

The other thing that can be done is to pass the variable around

public void doString() {

String s = doSomething("something");

}

private String doSomething(String s1) {

// do whatever to s1

return doSomething2(String s1);

}

private String doSomething2(String s2) {

// do whatever to s2

return s2;

}

Since both are correct (I presume), I'm wondering at what time would 1 way be preferred over the other? Or is 1 way more professional than the other?

Thanks.

sandridera at 2007-7-28 17:46:09 > top of Java-index,Java Essentials,Java Programming...
# 4

The answer to questions like this is usually "There is no answer". Most of the time it will be a design decision that the coder will have to make.

However, passing parameters can make your code more dynamic.

class DoubleUp {

int num = 2;

public void doubleA() {

System.out.println(num * 2);

}

public void doubleB(int value) {

System.out.println(value * 2);

}

}

Here you can see that method A will always produce the same result. Whereas method B will give different results depending on which value is passed.

floundera at 2007-7-28 17:46:09 > top of Java-index,Java Essentials,Java Programming...