-1

i have getUserData method in Main class. i want to call this method from the main class. can anyone help me?

public List<User> getUserData(){
    User user1 =new User(0,"Rezo Joglidze");
    User user2 =new User(1,"Galaktion Tabidze");
    User user3 =new User(2,"Ilia Chavchavadze");

    userList.add(user1);
    userList.add(user2);
    userList.add(user3);

    return userList;
}
ldz
  • 2,217
  • 16
  • 21

2 Answers2

0

Assuming that the method getUserData is located in a class called Main it can be called like follows:

public class Main {
    public static void main(String[] args) {
        List<User> userData = new Main().getUserData();
    }
}
ldz
  • 2,217
  • 16
  • 21
0

The way you can go about this depends entirely on your use case. Since the code you've provided doesn't need to pull from other classes in a non-static way, try using the static keyword to be able to call it from main. Try this out for size:

public class Main {
    public static void main(String[] args) {
        List<User> userData = Main.getUserData();
    }

    public static List<User> getUserData(){
        User user1 = new User(0, "Rezo Joglidze");
        User user2 = new User(1, "Galaktion Tabidze");
        User user3 = new User(2, "Ilia Chavchavadze");

        userList.add(user1);
        userList.add(user2);
        userList.add(user3);

        return userList;
    }
}
KuNet
  • 28
  • 2