1

To be honest i am really struggling with Java, i am trying to access an array from another class. I believe i have created the array in this code

public class aff_array {
        String name;
    public static void main (String[] args) {

        int z = 3; //total no of affirmations
        int x = 1;
        aff_array[] afz = new aff_array[z];  //dim

        while ( x < z ) {
            afz[x] = new aff_array();  // create objects for array
            x = x + 1;
        }

        afz[1].name = "i am the best";
        afz[2].name = "you are the rest";

    }

but i am really having trouble trying to figure out how i access afz[1].name for example, from another class. This is probably basic but i am really struggling..

5
  • 5
    This isn't specific to arrays. Your afz variable is a local variable - it's declared in main, and unless you pass the value to another method (or copy it to a field somewhere) nothing else will be able to see it. I suggest you read docs.oracle.com/javase/tutorial/java/nutsandbolts/… Commented Jan 19, 2014 at 9:53
  • 1
    Technically, declare it public String name... but you should probably do some Java learning instead of solving this particular problem... Commented Jan 19, 2014 at 9:57
  • Try posting the code for the other class (even though it doesn't work right). Show us more of the pieces you have and we will put them together for you correctly. Commented Jan 19, 2014 at 9:58
  • thanks guys, i am just trying to put a notification Commented Jan 19, 2014 at 10:04
  • Hello sunirmalya, remeber that if you find the right answer, you must check it as the accepted one, it is the way Stack Overflow works. Commented Mar 6, 2014 at 11:28

3 Answers 3

2

You cannot access it from another class as long as it is created as an automatic variable (in other terms, local variable). In the above code, your "afz" construct will be visible only inside the main method (and can be used only after it's instatiation). To make it visible for other classes you can define it as an instance variable. I.e:

public class aff_array {

    String name;
    aff_array[] afz;

public static void main (String[] args) {

    int z = 3; //total no of affirmations
    int x = 1;
    afz = new aff_array[z];  //dim

First, you should define it as private, and create a getter method (this is a common practice, to respect encapsulation), and then get it on the other class simply calling this method.

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

1 Comment

Thanks everyone, Endrik - that seems to be the answer i am looking for, i realise it is currently a local variable but i have another class where i am trying to do a notification and i want this to be the msg to feed that notification.
2

You can access public instance variable "name" of aff_array class

//this will print value of instance variable name
System.out.println(afz[1].name);

//If you want to modify the value of variable
afz[1].name = "modified name";

However this is not recommended. Protect your instance variable with private access modifier and access it using public getter, setter methods.

Example: public class aff_array { private String name; public String getName() { return this.name; }

    public void setName(String new_name)
    {
        //You can add some validations here
        this.name = new_name;
    }

    public static void main (String[] args) {

    int z = 3; //total no of affirmations
    int x = 1;
    aff_array[] afz = new aff_array[z];  //dim

    while ( x < z ) {
        afz[x] = new aff_array();  // create objects for array
        x = x + 1;
    }

    //To print name 
    System.out.println(afz[1].getName());

    //to set new value to name
    afz[1].setName("i am the best");
    afz[2].setName("you are the rest");

}

2 Comments

This is the other class and where i want the array value to replace public class OneShotAlarm extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { setNotification(context, "i want to replace this with value from array"); WakeLocker.acquire(context); Toast.makeText(context,"One shot alarm received. No more toasts will be shown.", Toast.LENGTH_SHORT).show(); WakeLocker.release(); }
sorry because i am so new they wont let me answer and so this is not formatted so you can read it..
1

So i have tried to add getters and it looks like this public class aff_array { String name; aff_array[] afz;

public aff_array[] getAfz() {
    return afz;
}

public String getName() {
    return name;
}

public static void main (String[] args) {

        int z = 3; //total no of affirmations
        int x = 1;
        aff_array[] afz = new aff_array[z];  //dim

        while ( x < z ) {
            afz[x] = new aff_array();  // create objects for array
            x = x + 1;
        }

        afz[1].name = "i am the best";
        afz[2].name = "you are the rest";

    }

This is the other class and where i want the array value to replace aff_array.class.getName() with aff_array[] getAfz() but i dont know how to do it or reference afz(1) for example (getName is working)

 public void onReceive(Context context, Intent intent)
    {

        setNotification(context, aff_array.class.getName());
        WakeLocker.acquire(context);
        Toast.makeText(context,"One shot alarm received. No more toasts will be shown.", Toast.LENGTH_SHORT).show();
        WakeLocker.release();
    }

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.