Java how to call method in another class

戏子无情 提交于 2021-02-04 21:39:15

问题


Sorry for the basic question but I'm learning, new to programming. I am trying to call a method that is in another class but I'm unable to without passing in the required values for the constructor of Cypher class. Do i really need a new instance of Cypher class each time i use a method in the Menu class and want to call a method of the Cypher class or how do I rewrite to avoid below.

public class Runner {
    private static  Cypher c;
    public static void main(String[] args)  {

        Menu m = new Menu();
        m.displayMenu();
        
        //c=new Cypher(3,2);// wont work unless i add this line
        c.displayCypher("text");

        
    }
}

public class Cypher {
    private int keyTotalRows;
    private int keyStartRow;
    
    
    public Cypher(int key, int offset) throws Exception {
        
        
    }

    
    public void displayCypher(String text) {
        
        System.out.println("Plain text:" + text);
        System.out.println("Key: Number of Rows:" + keyTotalRows + "\n" + "Key Start Row: " + keyStartRow);
        
    }
}

public class Menu {
            private Scanner s;
                private void enterKey() {
            s = new Scanner(System.in);
            System.out.println("Enter  key >");

            int key = Integer.parseInt(s.next());
            
            System.out.println("Enter offset >");

            int offset = Integer.parseInt(s.next());
            
            System.out.println(" Key:" + key + "\n" + " Offset:" + offset);

        }


        public static void displayMenu()  {
            System.out.println("Menu"); 
}

回答1:


You declare a Cypher type static variable c here. so you can't access the Cypher class method using c variable without object declaration. But you can call the c variable from any class because it's a static variable. But your Cypher class don't have any static Method so you can't call these methods without object initialization. So you must declare a static method or need to create an object for the access method from Cypher class.

But if you declare Cypher type static variable with initialization. then you can call c from any class and also called Chyper class method using the c variable reference. like:

// Declare this on Runner class
public static Cypher c = new Cypher();

// And then call from any class like 
Runner.c.yourMethod();

Happy Coding.




回答2:


Few things:. Your methods are private which means you cannot call them outside the class. change that and you will be able to create your object and call them.

Menu menu=new Menu() ;
menu.otherMethod();

However as the method calls c which is null you will get a null exception.

You should test for it and then call object c inner methods

If (this.c! =null)
{c.displayOtherCypherType("" ) ;}
else
{//do something here}

In order to be able to send Cypher from outside the class use a constructor that receives a Cypher object and assigns it to c or have a public set method that receives it and assigns it

public setCypher(Cypher cypher) 
{
(this.c=cypher) ;
}

Now If you want to call Cypher methods without instantiating it you can create a static method inside it and call Tha method. note that it to should be public or you won't be able to call it




回答3:


If you don't want to create an instance, you can make the method static.

Something like this:

public static void displayCypher(String text) {
        System.out.println("Plain text:" + text);
        System.out.println("Key: Number of Rows:" + keyTotalRows + "\n" + "Key Start Row: " + keyStartRow);
}

Then you can call this function in another class like

Cypher.displayCypher()


来源:https://stackoverflow.com/questions/63185120/java-how-to-call-method-in-another-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!