Priya
Priya

Reputation: 339

accessing a list from a static method in main class, in another method of different class in java

I have a question which may sound as basic question to experts. But I not an expert in java. So I am not knowing and unable to find much details also in google regarding this. Hope someone can help me out.

I want to access one List from a method which is present inside Main class to use in another method of different class in a different package. All access modifiers are public. Is this possible to access in java8?

public class Main {
public static void main(String[] args) throws IOException {
        initTariffData() ;
        ....
        ....
}
public static void initTariffData() {

  List<List<Object>> SlotList = null;
   .......
   .......
  for(int innerloop=0; innerloop<(CostList.size());innerloop++) {

                        newList = new ArrayList<>();

                        newList.add(PowerList.get(innerloop));
                        newList.add(DurationsList.get(innerloop));
                        newList.add(CostList.get(innerloop));

                        SlotList.add(newList);  //Lists of all slots for 7 days
                    }
          }
}

Another class :

public class MyModel implements TariffModel {

.....
//Here I want to access the List "Slotlist" from the function "initTariffData" in main class
....
}

I hope my question is clear and providing the necessary information. Please let me know if it is still not clear.

I have searched in google so much but there is not much information regarding accessing a variable from different method inside Main class to another package class.

Upvotes: 0

Views: 836

Answers (2)

NiVeR
NiVeR

Reputation: 9796

Not a direct answer to the question, but just few statements to convince you (hopefully) that what you are trying to do is not correct.

You are not reasoning in OOP optics. Java is object-oriented language and as such it suggests to isolate state and behavour in objects. If you have 2 objects, as it seems, and you are needing to access a particular property from the first one in the second one it means that the separation of concerns is not well done. Or perhaps these two objects have some things in common, in this case they should probably related in an inheritance tree.

As a general rule, objects should not directly expose their state to other objects, unless it is strictly necessary.

Upvotes: 1

Erwin Smout
Erwin Smout

Reputation: 18408

What may be accessible to code in another class, are members of a class (methods and fields). Your list is not a member of a class, it is a local variable in a method. It exists only while some thread is executing that method. If two or more threads are concurrently executing that method, there are equally many instances/appearances of that local variable. So in that case, which one would you want "accessed" and in the case there is none, what do you think "accessing it" should produce as a result ?

Upvotes: 1

Related Questions