Search code examples
javaarraylistscopecallindexof

Modify Arraylist Java, Scope issue


I'm working on a personal project, when I'm having trouble I code a 'simpify' version of it. I am assigning data to Arraylist, and I would like to access the index with indexOf(" "). I didn't use java for two years and I'm having trouble to get back to it.

Here is the main, call Plo.java:

import plo.tre;

public class Plo
{
    public static void main(String[] args)
    {
        tre test = new tre();

        // my problem
        StockDownloader.nouvel();
    }
}

And here is my tre.java an other class :

import java.util.ArrayList;

public class tre
{
    private ArrayList<String> open;

    public tre()
    {
        open = new ArrayList<String>();

        String s = "chaine1,chaine2, chaine a, chaine b, chaine c";
        String str[] = s.split(",");

        for (int i = 0; i < str.length; i++) {
            open.add(str[i]);
        }
    }

    public void nouvel()
    {
        System.out.println(open.get(0));
        int test = open.indexOf(" chaine b");
    }
}

What I am trying to do is: 1- call in the main the method nouvel, 2- have access to the Arraylist created in the same class but fill in a different method.

I know it seems silly but I couldn't find a solution.

Thanks you so much for your help.


Solution

  • In your main() method, you need to use the reference of the tre class (i.e., test) to call the nouvel() method as shown below:

    test.nouvel();
    

    Could you explain me what was the problem?

    You are using the class name StockDownloader (not sure where it is defined) to invoke the method nouvel(). Class names can only be used to call the static methods/members of a class. To call non-static methods, you need to use an object reference (like in your code test is called object reference for the new tre class object which you have created).

    So, the problem is that you need to use the object reference to invoke the class members of Tre class, which you are not doing. So use test (which is the object's reference) to call the nouvel() method.


    Also, I strongly suggest you rename the class to Tre (or much more meaningful) following Java naming standards i.e., class name should start with Uppercase so that the code will be readable.

    One more point is that the class constructors are meant for initializing the instance variables and your code is cluttered so you can refactor it as shown below:

    Tre class:

    public class Tre {
        private ArrayList<String> open;
    
        public Tre() {
            open = new ArrayList<String>();//just initialize ArrayList
        }
    
        public void loadData() {//add a new method to load ArrayList
            String s = "chaine1,chaine2, chaine a, chaine b, chaine c";
            String str[] = s.split(",");
    
            for (int i = 0; i < str.length; i++) {
                open.add(str[i]);
            }
        }
    
        public void nouvel() {
            System.out.println(open.get(0));
            int test = open.indexOf(" chaine b");
        }
    }
    

    Plo class:

    public class Plo {
        public static void main(String[] args) {
            Tre test = new Tre();//create object for Tre with ref variable as test
            test.loadData();//call loadData() method using test ref.
            test.nouvel();//call nouvel() method using test ref.
        }
    }