Using methods from another class in the main?

后端 未结 3 1174
悲&欢浪女
悲&欢浪女 2021-01-27 03:08

Okay so I did search this question and a decent amount of results showed up. All of them seemed to have pretty different scenarios though and the solutions were different for ea

相关标签:
3条回答
  • 2021-01-27 03:37

    If your method is a static, you can call ClassName.methodName(arguments);

    If your driver class not a static one you should create an instant of that class and call your method.

    ClassName className=new ClassName();
    className.displayEditParty(playerParty, tempEnemy);
    
    0 讨论(0)
  • 2021-01-27 03:41

    I dont see where your declaring the Driver class in your code.

    Driver foo = new Driver();
    foo.displayEditParty(playerParty, tempEnemy);
    
    0 讨论(0)
  • 2021-01-27 03:51

    You should make displayEditParty function public static and then you can use it in other class by className.displayEditParty(?,?);

    Methods of class can be accessible by that class's object only. Check below code:

    class A{
    
        void methodA(){
            //Some logic
        }
    
        public static void methodB(){
            //Some logic
        }
    }
    
    public static void main(String args[]){
    
        A obj = new A();
        obj.methodA(); // You can use methodA using Object only.
    
        A.methodB();  // Whereas static method can be accessed by object as well as 
        obj.methodB(); // class name.
    }
    
    0 讨论(0)
提交回复
热议问题