0

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"); 
}
Hulk
  • 6,399
  • 1
  • 30
  • 52
  • 1
    Can you please edit your question by adding the whole code including the class in which you want to call method. – Shiv Patel Jul 31 '20 at 02:58
  • Since `c` is a member of class `Menu`, you only need to call the `Cypher` class constructor once. How do you assign values to `keyTotalRows` and `keyOffset`? – Abra Jul 31 '20 at 03:01
  • Make the other method as static in other class and then you can directly call them with the help of class name like for example Math.max() here max is static method in math class. – gagangaur Jul 31 '20 at 03:03

3 Answers3

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

Gabriel H
  • 1,558
  • 2
  • 14
  • 35
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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Nayan Sarder
  • 512
  • 1
  • 3
  • 18
0

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()
Liu
  • 125
  • 7