Calling Static Method from Another Java Class

Calling static method from another java class

Ensure you have proper access to B.staticMethod. Perhaps declare it as

public static String[] staticMethod() {
//code
}

Also, you need to import class B

import foo.bar.B; // use fully qualified path foo.bar.B

public class A {
String[] lists = B.staticMethod();
}

Why I can't call a static method in another class?

You can't call method in the body of class, as you did it in class B. In the body of class you define fields and methods of this class. If you want some actions to be performed while creating instance of some class, you need to contain these actions in the constructor, in an intialization block or in the body of additional method. Calling methods in the constructor seems to be risky if object is not being created successfully in any circumstances, it might cause problems with calling method contained in the constructor.

To call methodA() I suggest one of the following ways to achieve that:

  1. Create appropriate method in class B and call static method of class A in the body of class B.

  2. Create a proper initialization block to call this method.

Examples of how to call methodA() from class B, you can see below:

// 1.:
class B {
public void callA() {
A.methodA();
}
}

or

// 2.:
class B {
{
A.methodA();
}
}

Call static method from another class without creating object in Java

Instead of doing

add(2,3)

Do

Test.add(2,3)

Can I call a static method of another class without using the class name?

Yes. But, you would need to import it. Specifically, import static. Like,

import static com.package.OtherClass.someMethod;

Then you can call someMethod(String) like

someMethod("Like that");

Is it possible to call a static method of another class from a non static method without instance in java?

firstly modifier static not allowed at class pgm.

If you want to call func and x in class legs.

You must use public final then your class name and declare all member of class as static.

After then you need to get reference for class pgm.

So your code will be

  import java.io.*;
import java.util.*;
public final class pgm
{
static int x,v;
static void func()
{
System.out.println("Function run");
}
}

class egs
{
public static void main(String args[])
{
pgm p=null; //ref here
p.func(); // use ppm func here
try
{
p.x=10;
p.func();
}
catch(NullPointerException e)
{
System.out.println("Null caught");
}

}
}

You would get what you want.

Never get confused static used for compile whole block, method, variable at compile time so you can call anything which is static at run time without any instantiation (using new).

Right way I provide you above.

Can't call a static method from another class to main class

If your main method is in a different class, then you need to specify the classname while calling the static method, i.e., NewTriangle.getnumberOfTriangles()

Generate code that calls static method from another class and uses several fields as arguments

You are calling a vararg method, java bytecode doesnt have that. So you need to create an actual array of the correct type to call the method.

@Override
public ByteCodeAppender appender(final Target implementationTarget) {
final TypeDescription thisType = implementationTarget.getInstrumentedType();

return new ByteCodeAppender.Simple(Arrays.asList(ArrayFactory.forType(TypeDescription.Generic.OBJECT)
.withValues(Arrays.asList( //
new StackManipulation.Compound(MethodVariableAccess.loadThis(),
this.getField(thisType, "field1")),
new StackManipulation.Compound(MethodVariableAccess.loadThis(),
this.getField(thisType, "field2")))
), MethodInvocation.invoke(SIMPLE_CACHE_KEY_OF) //
, MethodReturn.of(TypeDescription.STRING)));

}

Maybe byte-buddy has a special builder for that, but at least thats one way of doing that.

Imo: it is often a good approach to write a java version of the bytecode you want to generate. That way you can compare the javac bytecode and bytebuddy bytecode.

How to call non-static method in another class's static method in java?

Pass instance of your class containing non-static methods into static method

public static void searchResEntry(YourClass instance) throws java.io.IOException    // start of method
{
// variable declarations
instance.liveFlights(...);

or create instance in it:

public static void searchResEntry() throws java.io.IOException    // start of method
{
// variable declarations
YourClass instance = new YourClass();
instance.liveFlights(...);

Can I call a static method of one class in another class under same package?

You can call a method with the syntax ClassName.methodName() if the method is declared static, eg

class ClassName {
static void methodName() {
//...//
}
}

More info about static class members can be found in the Java Tutorials.



Related Topics



Leave a reply



Submit