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();
}
}