How do you call a method from a different class (file) in java?
Are objects required? Or is there a 3rd way to make a merge of documents for java? Can we use a simple method call of is there a proper way to call new methods?
Your question is not clear to me, as far as I understand you want to call a method of another Java file (I assume another Java class).
So consider you have java files A.java and B.java. So you have definitely two classes A and B.
Now if you want to call a method of B class from A class you need to:
Take a look:
• Non-static method:
B.java
class B {
public void myMethod() {
// do stuff
}
}
A.java
class A {
public void anotherMethod()
{
B b=new B();
b.myMethod(); // calling B class's method
}
}
• STATIC method:
B.java
class B {
public static void myMethod() {
// do stuff
}
}
A.java
class A {
public void anotherMethod()
{
B.myMethod(); // calling B class's method
}
}
you can try packaging your class
using package com.yourwebsite; at the very start of your class and above import statements
then at the cmd type javac -d . YourClass.java
then you can import it on the next class that you are trying to create
to import it use import com.yourwebsite.YourClass;
note that your package doesn't allow wild card symbol so you must use the full name of your class
Are objects required?- in Java every method is at least located in a class. To call static methods you need the class, to call instance methods you need an instance of that class.