How do you call a method from another class?

1

Hello, sorry, I have a problem in my java studio?

How to call a method from another class? Example:

public class metodos
{
   public static void mensaje(){
      System.out.println(" hola mundo ");
  }
}

example I have this

For example: This static method and return value void which do not need to create an object of this class to call this method as you might call it? With extends or as

    
asked by Sommer 07.10.2016 в 03:35
source

4 answers

6

In java it is important to write the modifiers correctly, in this case Public must be written with lowercase public .

I recommend this article: public, private and protected Java.Types of access modifiers.

It is important to write your code correctly since otherwise you will have problems when trying to compile. By the way, the names of classes by convention are written at the beginning with capital letters (see basic syntax ).

class Metodos{
public void mensaje() (
   System.out.println("hola mundo");
   )
)

To call the message method () of this class

public class Metodos{
public void mensaje() (
   System.out.println("hola mundo");
   )
)

If it was not defined as static , from the previous class from a class you simply do it in this way, instances the class, and you send the method, it is important that it be defined as public to be called from another class (see access modifiers ):

//Instancias la clase.
Metodos myClase = new Metodos();
//ejecutas el metodo de la clase.
myClase.mensaje();

An example calling from the main class

class ClasePrincipal {
    public static void main(String[] args) {
        //Instancias la clase.
        Metodos myClase = new Metodos();
        //ejecutas el metodo de la clase.
        myClase.mensaje();
    }
}
public class Metodos{
public static void mensaje() (
   System.out.println("hola mundo");
   )
)

To call the message method () which is defined as static , simply:

Metodos.mensaje();

An example calling from the main class

class ClasePrincipal {
    public static void main(String[] args) {
        //ejecutas el metodo de la clase.
        myClase.mensaje();
    }
}
    
answered by 07.10.2016 в 04:09
1
nombreDeLaClase.nombreDelMetodo();

From other classes as long as the method is public.

    
answered by 07.10.2016 в 04:06
1

We create an object of that class in the following way

Metodos nombreObjeto = new Metodos();

Then we use the object to call the methods (in this case the publics) of that class.

nombreObjeto.mensaje();
    
answered by 12.09.2018 в 21:46
0

Suppose we have these 2 classes.

Firstly "Operation1":

public class Operacion1{
    public static int sumar(int a, int b){
        return a + b;
    }
}

And then "Operation2":

public class Operacion2{
    public int sumar(int a, int b){
        return a + b;
    }
}

We are going to do a main method and it will look easier:

public class Test{
    public static void main(String[] args){


        System.out.println(Operacion1.sumar(5,7)); // Imprimirá 12

        // Ejemplo con Operación2
        Operacion2 operacion = new Operacion2();
        System.out.println(Operacion2.sumar(5,7)); // Imprimirá 12
    }
}

If the method is static, then we call it with the first example NombreDeLaClase.metodo() , and if not, we will have to instantiate the class (we will be creating an object) and then to the operation variable we call the add method. The way to do it is nombreDeLaVariable.metodo() .

    
answered by 08.10.2016 в 00:59