0

I got a array of objects. From another method i want to print one object from the array, the input to this method must be an integer, that represent the index of the object in the array. I can't reach the array from printObject(). How do i do this?

public static void main(String[] args) {
    Object []obj = new Object[2];
    printObject(1);
}

public static void printObject(int i){
    if (i == 0){
        System.out.println(obj[0].toString());
    }
    if (i == 1){
        Systen.out.println(obj[1].toString());
    }
}
0

5 Answers 5

1

You could pass the array to printObject as a parameter (and simplify):

public static void main(String[] args) {
    Object[] obj = new Object[2];
    printObject(obj, 1);
}

public static void printObject(Object[] objects, int index){
    if (index == 0 || index == 1) {
        System.out.println(objects[index].toString());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Because it's declared inside the block of the main method, it will be known only there. Make it a class member or pass it as a parameter.

Example:

private int memberInt;

private void foo() {
   memberInt = 5; // :)
   int a = 7;
   //..
   a = 9; // :)
}

private void bar() {
   a = 10; // :_(
   memberInt = 10; // :)
}

Comments

1

The scope of the variable obj is limited to main method and will not be available in printObject method. So to get access to variable of type Object[], make Object []obj as class member so that this member will be available through out the class or can be sent as an argument to printObject method.

Check the following code:

public class AccessingMembers 
{
static Object []obj  = null;

public static void main(String[] args) {
    obj = new Object[2];
    obj[1] = new Integer(10);//for example
    printObject(1);
}

public static void printObject(int i){
    if (i == 0){
        System.out.println(obj[0].toString());
    }
    if (i == 1){
        System.out.println(obj[1].toString());
    }
}
}

If you run the code you'll get 10 as an answer.

Comments

0

either declare a global array which is accessible throughout the class or pass the array as a paramter to the method, so that it can access it.

Comments

0

Object []obj = new Object[2]; is a method variable and it's scope is only to that method.

Here there is one more thing using the above statement you created only two references of the object but not the instances.

//create instances

obj[0]=new Object();
obj[1]=new Object();

try this,

class Test {
static Object[] obj = new Object[2];

public static void main(String[] args) {

    printObject(1);
}

public static void printObject(int i) {
    obj[0]=new Object();  
    obj[1]=new Object();  

    if (i == 0) {
        System.out.println(obj[0].toString());
    }
    if (i == 1) {
        System.out.println(obj[1].toString());
    }
}

}

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.