Java: How to Access Methods from Another Class

Java: How to access methods from another class

You need to somehow give class Alpha a reference to cBeta. There are three ways of doing this.

1) Give Alphas a Beta in the constructor. In class Alpha write:

public class Alpha {
private Beta beta;
public Alpha(Beta beta) {
this.beta = beta;
}

and call cAlpha = new Alpha(cBeta) from main()

2) give Alphas a mutator that gives them a beta. In class Alpha write:

public class Alpha {
private Beta beta;
public void setBeta (Beta newBeta) {
this.beta = beta;
}

and call cAlpha = new Alpha(); cAlpha.setBeta(beta); from main(), or

3) have a beta as an argument to doSomethingAlpha. in class Alpha write:

public void DoSomethingAlpha(Beta cBeta) {
cbeta.DoSomethingBeta()
}

Which strategy you use depends on a few things. If you want every single Alpha to have a Beta, use number 1. If you want only some Alphas to have a Beta, but you want them to hold onto their Betas indefinitely, use number 2. If you want Alphas to deal with Betas only while you're calling doSomethingAlpha, use number 3. Variable scope is complicated at first, but it gets easier when you get the hang of it. Let me know if you have any more questions!

Java how to call method in another class

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.

How to access a method from another class outside the creator of the instance

The instances only exist within the method they're declared, in this case the constructor. A common way of getting around this is to declare a field in your class, and assign the value of that field in the constructor (or other method). Try:

package main;
public class Main {
// private GUI gui; // replaced this line with the below. See comment 5
private static GUI gui; // this is the new field declaration

public static void main(String[] args) {
gui = new GUI(); // note I removed the class declaration here since it was declared above.
gui.setVisible(true);

Serial serial = new Serial();
serial.getPorts();
fillList();
}

public void fillList () {
gui.setList("hoi"); // now this method has access to the gui field
}
}

Can't call method from another class in Java

You have to do something like below:

public class Persona {
...
//You should have instance of RegistroCompra
RegistroCompra registraCompra = new RegistroCompra();
public void setDestino() {
//Option 1: Explicitly call the method
registraCompra.seleccionarDestino();
destinoPasajero = registraCompra.obtenerDestino();
}
}

public class RegistroCompra {

private String destino;

public RegistroCompra(){
//Option 2 : Call the method in constructor
registraCompra();
}
public void seleccionarDestino() {
...
//Set the input to the class level variable destino
this.destino = input.nextLine();
}
public String obtenerDestino() {
return this.destino;
}

}

How to use methods from another class, called by the client

Creating and using a class by name requires some preconditions:

  • Full name of the class must be used, including package name.
  • You need to know the parameters of the constructor. Usually the no-arguments constructor is used for a such use case.
  • (Optional) You need an interface this class must implement which declares the method "run" (or any other methods you want to use).

An example with a subclass of Runnable:

    String className = "some.classname.comes.from.Client";

Class<Runnable> clazz = (Class<Runnable>) Class.forName(className);
Runnable instance = clazz.getConstructor().newInstance();
instance.run();

If you don't have a common interface, you can invoke the method using reflection:

    Class<Object> clazz = (Class<Object>) Class.forName(className);
Object instance = clazz.getConstructor().newInstance();
clazz.getMethod("run").invoke(instance);

or using method with parameters:

    Integer p1 = 1;
int p2 = 2;
clazz.getMethod("methodWithParams", Integer.class, Integer.TYPE).invoke(instance, p1, p2);

Calling method of Another class from run() method

A common problem with ScheduledExecutorService is that if the scheduled command throws an exception, the exception is silently swallowed. The code in the question does not seem to throw exceptions, but in more complex code it is difficult to avoid (for example unexpected NullPointerExceptions)

A quick fix is to put all of the Runnable.run method in a try-catch block:

public class SomeMinuteJob implements Runnable {
@Override
public void run() {
try {
System.out.print("Inside run method");
ReadData obj = new ReadData();
obj.readData();
System.out.println("After reading data");
} catch (Exception e) {
// Log exception
e.printStackTrace();
}
}
}


Related Topics



Leave a reply



Submit