1

I have two programs. I create a static array and some methods such as the following:

public Someclass{
    static int counter[] = new int[n];

    //methods & main

}

the n is defined to be some number, so I know it will have some length. I later fill this array and I test to see that it gets filled correctly, so I know some indexes should have values other than 0. Now when I try to call it in second program, it is though I never filled it because it only gives me 0's.

  //second program 
  public Someclass2{

      public static main(String[] args){
         String n = "someword"
         int[] nums = new int[n.length]
         for( int i = 0; i < n.length; i++){
            nums[i] = nums[i] + (25 * SomeClass.counter[i]); 
          }
      }

 }

For some reason, when I call the array in the second program it returns all zeros and doesnt change the value of nums, even though I know the counter array should have non-zero values. I think it has to do with the fact that I initialize it statically but I filled it in a local method and in the class. So techincally it never gets it's zeroes updated. I am having trouble trying to fix this and if anyone could help I would aprreciate it.

Thank you

2
  • when are you calling the local method in your SomeClass which modifies the counter array? Commented Sep 14, 2014 at 17:55
  • static arrays are initialized to all zeros. You are not putting values in it . If you have processed values. There is no call for it in your main method. Commented Sep 14, 2014 at 17:55

4 Answers 4

2

My guess: you're filling the array inside of SomeClass, but never call that filling code before using the array in the other class. Solution: be sure to fill it first. For more specific help, show us more details about your code.

Other points -- it is usually best to avoid use of static fields, and instead it is usually better to make the array an instance field. Then you can ascertain its state through its containing class, and also change its state through the containing class with any restrictions that you'd like to put on the ability to change it.

Sign up to request clarification or add additional context in comments.

Comments

0

If the second 'program' is really a separate program and not just another class, then it won't work the way you want it to. When the second program calls the static class, it will just create a new array for the second program, while the original array you filled in the first program persists for the first program. Static variables are only accessible to other parts of the same program, not all programs running on the computer.

Comments

0

The problem is here: nums[i] = nums[i] + (25 * SomeClass.counter[i]);

Basically you have created an array and haven't populated it.
So if n = 5 then counter == {0,0,0,0,0} .

This means that each index gets the default value of 0 so everytime you do this :
25 * SomeClass.counter[i] you multiply with 0 which of course returns 0

Comments

0

There is nothing that happens in Someclass2.main() that would cause the static array in Someclass to be filled. It is all zeroes because that is how the Java spec defines it:

Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10) [...] For type int, the default value is zero, that is, 0.

The second program never does anything to modify the state of Someclass.counter resulting in getting all zeroes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.