user9679178
user9679178

Reputation:

Variable is reset after loop

I'm learning Java and have written a program with a for loop in it. But my variable is 0 after I print it out. Why does it get reset? This is my code:

Private static int number;

public static void main(String[] args)
{
    int number = 0;
    for (int i = 0; i < 10; i = i + 1)
    {
        number = number + 1;
    }
    System.out.println("Main: " + number);
    print();
}

public static void print()
{
    System.out.println("Print: " + number);
}

This is the result:

run:

Main: 10

Print: 0

BUILD SUCCESSFUL (total time: 0 seconds)

Upvotes: 0

Views: 3357

Answers (6)

N.Ingham
N.Ingham

Reputation: 11

Your issue is to do with the scope of your variables. If you want to keep a static 'number' variable then don't include 'int' when you initialize it in the 'main' method. (see code below)

private static int number;

public static void main(String[] args)
{
  number = 0; //you are now initialising the global static variable and not a local variable to the main method.
  for (int i = 0; i < 10; i = i + 1)
  {
    number = number + 1;
  }
  System.out.println("Main: " + number);
  print();
}

public static void print()
{
  System.out.println("Print: " + number);
}

OR

Personally, I prefer to avoid using global variables. you can pass 'number' in as a parameter to the 'print' method instead.

public static void main(String[] args)
{
  int number = 0;
  for (int i = 0; i < 10; i = i + 1)
  {
    number = number + 1;
  }
  System.out.println("Main: " + number);
  print(number);
}

public static void print(int number)
{
  System.out.println("Print: " + number);
}

Upvotes: 0

Yan
Yan

Reputation: 433

You have 2 different variables named number.

One is Private static int number and another one is inside the main function int number = 0.

I believe you wanted the static member to be changed inside your loop, thus you have to remove the declaration from the main function.

Private static int number;

public static void main(String[] args)
{
    number = 0;
    for (int i = 0; i < 10; i = i + 1)
    {
        number = number + 1;
    }
    System.out.println("Main: " + number);
    print();
}

public static void print()
{
    System.out.println("Print: " + number);
}

Upvotes: 0

olejnik.tech
olejnik.tech

Reputation: 61

you re-declared int number inside main() method. It is like to create new variable.

Just delete 'int' and it should work.

Private static int number;

public static void main(String[] args)
{
    number = 0;
    for (int i = 0; i < 10; i = i + 1)
    {
        number = number + 1;
    }
    System.out.println("Main: " + number);
    print();
}

public static void print()
{
    System.out.println("Print: " + number);
}

Upvotes: 0

Michael
Michael

Reputation: 44150

You have two variables with the same name. One is a local variable which is updated in the main loop:

public static void main(String[] args)
{
    int number = 0;
    //...
}

The other is a static variable which is initialised to zero:

Private static int number;

(by the way, Private should be lowercase)

Your print method is using the static variable (which never changes). You can fix this by passing a parameter to the print function:

public static void print(int number)
{
    System.out.println("Print: " + number);
}

and call that from your main method like so:

print(number);

You should also remove the static variable. It's unnecessary.

Upvotes: 0

k_ssb
k_ssb

Reputation: 6252

You have two declarations of number, one as a private static class variable, and one as an local variable in main. The local variable in main is hiding the other variable, so the static variable isn't being updated at all.

Upvotes: 0

Vallas
Vallas

Reputation: 503

Your variable is not reset. This might be hard to grasp when you're new but by saying int number; you create a new variable number of type int. Also keep in mind that variables will remain in the scope where they were created and can't be used outside that scope. This means that if you initialize a variable in a while loop, it will only be available from within that while loop. The same applies to methods.

You created a variable number on the first line, this variable can be used by every method in your class and is set to zero by default. However, you created another variable number in your main method. This second variable only exists within your main method because that's the scope where it was created in.

Let me give you some ways to fix this:

Private static int number;

public static void main(String[] args)
{
    //By removing `int`, we reference the variable that has already been initialized
    //instead of creating a new one
    number = 0;
    for (int i = 0; i < 10; i = i + 1)
    {
        number = number + 1;
    }
    System.out.println("Main: " + number);
    print();
}

public static void print()
{
    System.out.println("Print: " + number);
}

This is a possibility if your variable doesn't have to be accessed any further:

public static void main(String[] args)
{
    int number = 0;
    for (int i = 0; i < 10; i = i + 1)
    {
        number = number + 1;
    }
    System.out.println("Main: " + number);
    print(number);
}

//We use the created variable as a parameter
public static void print(int number)
{
    System.out.println("Print: " + number);
}

Upvotes: 1

Related Questions