calling static array from another class

overboard182

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

Hovercraft Full Of Eels

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling static method from another java class

From Dev

calling a static parameter from one public class to another

From Dev

calling a static parameter from one public class to another

From Dev

PHP: Get class name of static method calling another static class

From Dev

Calling a JMenuBar from another class

From Dev

Calling Volley from another class

From Dev

Calling an object from another class

From Dev

Calling a function from another class?

From Dev

Calling another JFrame/JPanel from another Class

From Dev

Calling a class from another class with main method

From Dev

Calling function from one class in another class

From Dev

Calling a method from one class in another class

From Dev

PHP - Calling database class from another class

From Dev

calling static method from inside the class

From Dev

Calling static methods from a Class type

From Dev

Calling Array from class PHP

From Dev

Calling trait static method from another static method (rust)

From Dev

calling non static method from static class in java with spring autowiring?

From Dev

PHP: is it possible to call a static class method from another static class?

From Dev

Calling another controller from service class in spring

From Dev

Calling a method of the MainActivity from another class?

From Dev

Calling a method from inside of another class

From Dev

Having an issue calling methods from another class

From Dev

Calling Function from another class swift

From Dev

Calling the paint() method from another class?

From Dev

Calling getter methods from another class

From Dev

Trouble calling method from another class

From Dev

Trouble calling a method from another class

From Dev

Calling instance method from another class ruby

Related Related

  1. 1

    Calling static method from another java class

  2. 2

    calling a static parameter from one public class to another

  3. 3

    calling a static parameter from one public class to another

  4. 4

    PHP: Get class name of static method calling another static class

  5. 5

    Calling a JMenuBar from another class

  6. 6

    Calling Volley from another class

  7. 7

    Calling an object from another class

  8. 8

    Calling a function from another class?

  9. 9

    Calling another JFrame/JPanel from another Class

  10. 10

    Calling a class from another class with main method

  11. 11

    Calling function from one class in another class

  12. 12

    Calling a method from one class in another class

  13. 13

    PHP - Calling database class from another class

  14. 14

    calling static method from inside the class

  15. 15

    Calling static methods from a Class type

  16. 16

    Calling Array from class PHP

  17. 17

    Calling trait static method from another static method (rust)

  18. 18

    calling non static method from static class in java with spring autowiring?

  19. 19

    PHP: is it possible to call a static class method from another static class?

  20. 20

    Calling another controller from service class in spring

  21. 21

    Calling a method of the MainActivity from another class?

  22. 22

    Calling a method from inside of another class

  23. 23

    Having an issue calling methods from another class

  24. 24

    Calling Function from another class swift

  25. 25

    Calling the paint() method from another class?

  26. 26

    Calling getter methods from another class

  27. 27

    Trouble calling method from another class

  28. 28

    Trouble calling a method from another class

  29. 29

    Calling instance method from another class ruby

HotTag

Archive