O
O
olololosh2015-03-18 03:08:50
Java
olololosh, 2015-03-18 03:08:50

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

5 answer(s)
M
Michael, 2015-03-18
@cocain1988

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 in more detail it is certainly better to read the documentation on those links that were indicated earlier.

V
Vladimir Abramov, 2015-03-18
@kivsiak

Go through the tutorial?
docs.oracle.com/javase/tutorial

L
localghost, 2015-03-18
@localghost

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.

M
Maxim, 2015-03-18
@Qumbeez

Banal class inheritance.

I
Ilya Bobkov, 2015-03-18
@heksen

put one instance of a class into another and call

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question