0

Thanks to those that helped me get this far. I made some changes. Now there is an error on the line where array1 is assigned in the method, Method(). (edited version show below)

public class JavaApplication2 {

int[] array1;
public JavaApplication2()
{
}

public static void main(String[] args) {
    JavaApplication2 obj = new JavaApplication2();
    obj.method();
    System.out.print(obj.array1[1]);
}

public void method()
{
    array1 = {1,1,1,1,1,1};
}

}

SilverF0x
  • 67
  • 1
  • 13

4 Answers4

1

Do this:

public class JavaApplication2 {
    int array[];

    public static void main(String[] args) {
        JavaApplication2 obj = new JavaApplication2();
        obj.method();
        System.out.print(obj.array[1]);
    }

    public void method() {
        array = new int[]{1,1,1,1,1,1};
    }
}

You declarated an array in the constructor of the class and one in the method() method. These are local variables which are only accessible in that particular method/constructor. You need to specify an instance variable which can be accessed in the way you tried.

fatih
  • 1,395
  • 10
  • 9
  • You would do well to provide an explanation as to *why* your answer works. – Sinkingpoint Jan 27 '14 at 20:19
  • I get "illegal start of expression warning" error on the line "array = {1,1,1,1,1,1}; when I try to do it that way. Is there something keeping it from accessing the array declared in the class, or is there a formatting issue? – SilverF0x Jan 27 '14 at 21:00
1

They are defined within the same class; however they are declared within methods so they are local variables. This means they only exist within their respective methods and in particular only for a single invocation of the method. Essentially they disappear when the method exits and are recreated anew each time the method is called.

To quote the tutorial:

  • Local Variables Similar to how an object stores its state in fields, a method will often store its temporary state in local variables. The syntax for declaring a local variable is similar to declaring a field (for example, int count = 0;). There is no special keyword designating a variable as local; that determination comes entirely from the location in which the variable is declared — which is between the opening and closing braces of a method. As such, local variables are only visible to the methods in which they are declared; they are not accessible from the rest of the class.

You probably mean for them to be fields (also called instance variables), which means that each instantiated object of the class has their own 'personal' variable which persists along with the object that 'owns' it. Fields are declared inside the class but outside a method.

public class JavaApplication2 {
    int[] array1; // variable declaration
                  // begins with the type of the variable

    public JavaApplication2() {}

    public void method() {
        array1 = {1,1,1,1,1,1}; // assignment
    }                           // begins with an identifier
}

Typically fields are initialized once during the constructor so you don't have to call a separate method to do so.

public class JavaApplication2 {
    int[] array1;

    public JavaApplication2() {
        array1 = {1,1,1,1,1,1};
    }
}
Radiodef
  • 37,180
  • 14
  • 90
  • 125
0

You need to create array1 as a class variable in order for the whole class to have access to it. As a general rule of thumb, the scope of a variable last between the two brackets { } it is initialized in. In your two methods, you initialize array1 in those method, so the scope of array1 is in those methods only.

Change the code to something like this:

public class JavaApplication2 {

    int[] array1;

    public JavaApplication2()
    {
    }

    public static void main(String[] args) {
        JavaApplication2 obj = new JavaApplication2();
        obj.method();
        System.out.print(obj.array1[1]);
    }

    public void method()
    {
        array1 = new int[]{1, 1, 1, 1, 1};
    }
}

It would be even better to initalize array1[] as private and have a getter method for it like this:

public class JavaApplication2 {

    private int[] array1;

    public JavaApplication2()
    {
    }

    public static void main(String[] args) {
        JavaApplication2 obj = new JavaApplication2();
        obj.method();
        System.out.print(obj.getArray1().[1]);
    }

    public void method()
    {
        array1 = new int[]{1, 1, 1, 1, 1};
    }

    public void getArray1()
    { 
        return array1;
    }
}
mdewitt
  • 2,526
  • 19
  • 23
  • I get an error on the line "array1 = {1,1,1,1,1,1};" when I try to run this... which is why I declared it the way I did in the first place. Any idea how to fix it? – SilverF0x Jan 27 '14 at 20:56
  • I had a typo in the array1 intialization which might have been the problem. I just fixed it try now. If it still doesn't work, what error do you get? – mdewitt Jan 27 '14 at 21:07
  • @beandaddyo Try it now. I mistyped the creation of the array in the `method()` method. – mdewitt Jan 27 '14 at 21:22
-1

Because it's null, you didn't initialize it. Inside Method you are initializing other array, not what you are asking for.

Thrash Bean
  • 658
  • 4
  • 7
  • You can't initialize (or access for that matter) something that doesn't exist in the first place. – Sinkingpoint Jan 27 '14 at 20:20
  • Isn't answer? And why not? Dont' understand. I cant comment the answer of fatith but this will get an compiler error. – Thrash Bean Jan 27 '14 at 20:22
  • On the line "System.out.print(obj.array[1]);" hi is asking for array that is not initialized, thinking is asking for array1, as it is a field. And what is "the first place"? – Thrash Bean Jan 27 '14 at 20:24
  • There is no array1 field in the class. Thus you can't initialize it. – Sinkingpoint Jan 27 '14 at 20:25
  • What array2? There is nothing like array2. The user has edited the post but wont work. – Thrash Bean Jan 27 '14 at 20:26
  • Lets just say, this is not an initialization problem. It is a trying to access a field that doesn't exist in the scope of the class problem. – Sinkingpoint Jan 27 '14 at 20:28
  • I don't agree. Being initialized the array "array" he could acces it. Declaring another array in the method and asking for the fist one there is a problem. However, thank you for the downvote, was completely unnecesary. – Thrash Bean Jan 27 '14 at 20:32
  • The downvote was necessary. Have one from me. You don't understand OP's question, and we don't want to confuse the OP. Its not a matter of whether the array is null. The array is not defined within the correct scope, therefore it cannot be accessed. Yes, the array isn't null. But that is irrelevant in this question. –  Jan 27 '14 at 20:38