0

I am working in JAVA Maven project. I have one class

package com.fist.program;

import java.util.ArrayList;
import java.util.Arrays;


public class BundleVersionManagement {

    public static ArrayList<String> BundleVer = new ArrayList<String>();
    public static ArrayList<String> BundleName= new ArrayList<String>();

    public BundleVersionManagement(){

    }

    public void SetBundleVersion(String BundleVersion) {
        if(BundleVer.contains(BundleVersion)==false){
            BundleVer.add(BundleVersion);
        }
    }

    public void SetBundleName(String Bndnm) {
        if(BundleName.contains(Bndnm)==false){
            BundleName.add(Bndnm);
        }
    }

    public void  DisplayArr(){
        System.out.println("common program ::: BundleVersionManagement::: bundle versions :");
        System.out.println(Arrays.toString(BundleVer.toArray()));
        System.out.println("common program ::: BundleVersionManagement::: bundle name :");
        System.out.println(Arrays.toString(BundleName.toArray()));
    }
}

Above class it's working fine. I want to use above static arraylist in another package class.

package com.first.secondProgram;

import com.fist.program.BundleVersionManagement;

public abstract class Handler<T> {

    static{

        BundleVersionManagement hbm = new BundleVersionManagement();
            NameFromxml= hbm.BundleName;
            System.out.println("common handler ::: Print array list:");
            hbm.DisplayArr();
    }
}

Problem is that class BundleVersionManagement add arraylist successfully as per log :

[1.123, 2.057, 3.018, 4.012, 5.018, 6.011, 7.119]

But when I print same arraylist in Hanlder class of another package, it's display blank :

common handler ::: Print array list: common util :::
BundleVersionManagement::: bundle versions : [] common util :::
BundleVersionManagement::: bundle name : []

Can you please help me how to access array list string another package.

3
  • We can see no place where you add element to the lists ;) also please follow convention and name your variable in camelCase (start lowercase) :) Commented Dec 4, 2017 at 8:33
  • 2
    This whole setup looks really dangerous. Are the elements of the name and version array related? Does the first bundleName go with the first bundleVersion, etc? If so this is in serious need of redesign. Also the use of public static is a red flag. Sounds like you should be using dependency injection instead. Commented Dec 4, 2017 at 8:38
  • Unrelated to the question: your method names do not follow Java code conventions. They're required to be camelCased Commented Dec 4, 2017 at 8:39

1 Answer 1

1

There is nothing wrong with your access of arraylist as such - in the second case you just haven't yet added any items into it.

A static block will run before any instance methods are run, so you probably are adding your items later than that. If you call the DisplayArr method after you've added your items, it will work.

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

9 Comments

@Hkachhia why are you doing printing in a static block anyway? Just do the printing code where you actually intend to use it. It shouldn't be in a static block in any normal case.
And if(BundleVer.contains(BundleVersion)==false) {...} could simply be written as: if(!BundleVer.contains(BundleVersion)) {...}
@eis: I have print array in hanlder constructor and still it's displaying blank.
@Hkachhia well, you need to print the array after you've added any items into it. You're still printing it before.
@eis: Actually my requirement is that, I want to assign array in handler local array list. But before Assign to handler array I have print array for verify.
|

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.