Answer the question
In order to leave comments, you need to log in
How can I call a method from another class?
There are two classes, one of which has a main method, the other class also has methods. Like me from the class. where is the main method to call methods of another class?
Answer the question
In order to leave comments, you need to log in
If I understand correctly, then you need to first declare the class whose method you want to call. For example like this:
class Apple{
public String color;
public String color(){ return color}
}
class Shop{
public static void main(String[] args){
Apple a = new Apple();
System.out.println( a.color() ); // Данный метод должен вывести цвет.
}
}
And somehow no one, in my opinion, even hinted at the essence.
KMK, there is not enough understanding that (usually) you cannot call just a class method. A class is a template from which objects are created. To call a "class method", you need to create an object of this class, and then refer to the object, they say, dear, call your method such and such.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question