rainer
rainer

Reputation: 3411

Why does the button object behave differently depending on where it is declared

I am writing a Android App in Java (with Codename One). I am into Java for about 3 years (Swing, FX, after a year of Python), but not a professional programmer. So, for the pros around, kindly bear with me.

Here is my issue: I have noted that a project can run quite differently, depending on if a var is initiated inside a for-loop or at the beginning of a class.

Here is some code:

In the first scenario, a button is instantiated inside the for-loop, either calling the button, or adding it via a list. When running the project, the results are identical :

for (int i = 0; i < list.size(); i++) {

        Button button = new Button("test"); // OR
        Button button = new Button(list.get(i));
        form.add(button);
 };

In the second scenario, the button is instantiated outside the for-loop. In this scenario, the result is different when I use an ArrayList, or a button.

public class Test {

    private Button button;

    public void animation() {       

        for (int i = 0; i < list.size(); i++) {

            button = new Button("test");        // OR
            button = new Button(list.get(i));
            form.add(button);
        }
    }
}

There is also a timer in the project, and when I add a button with the second approach, the components show up in an uncoordinated manner, what does not occur when I use the fist approach.

I have solved the programming part, but, independently of the immediate consequences on the project, why is it that in Java it can matter at which point, inside or outside a for-loop, you instantiated a variable? And why does it matter if I use an ArrayList or not?

To me, it doesn't make sense. Is this unexpected java behavior, or is there something going on under the hood, that I don't know of?

Anybody can explain?

Upvotes: 1

Views: 113

Answers (1)

prasad_
prasad_

Reputation: 14297

Here is my issue: I have noted that a project can run quite differently, depending on if a var is initiated inside a for-loop or at the beginning of a class.

Some clarifications:

Scenario 1:

for (int i = 0; i < list.size(); i++) {

        Button button = new Button("test"); // OR
        Button button = new Button(list.get(i));
        form.add(button);
 };

Consider the statement: Button button = new Button("Press me");

new Button() creates a new Button object and is assigned (the =) to the variable button. button is a reference variable of type Button. And, button refers or points to a memory location. Note the above code is within a method and also within a block (for-loop) - so button is a local or method variable.

For each iteration of the for loop a new Button object is created and the reference (memory location) is stored or added to the form. If the for loop runs 3 times there are three different Button objects and three references.

Scenario 2:

private Button button;

The above statement is declaring a button reference variable (of type Button). It is an instance variable; there is only one instance of it for an (one) object of the class it is declared in. This means, whenever an object of the class Test is created (for example, Text test = new Test(), or new Test()), there will be only one variable button and one location in the memory.

public void animation() {       

    for (int i = 0; i < list.size(); i++) {
        button = new Button("test");
        form.add(button);

The first time the code button = new Button("test"); is run, in the for-loop, the instance variable button is assigned a new Button object (new Button("test")). This points to the memory location of the button variable. For the second iteration of the for-loop another new Button object is created and is assigned to the same button variable (memory location), etc.

So, the button instance variable's memory location is re-assigned again and again and that is the reason you see this "un-coordinted" behaviour.

NOTES:

See this post authored by me: Object References in Java.

Also, try to know what is a heap memory in Java.

Upvotes: 2

Related Questions