Public Static Void Does Not Work Properly - NETBEANS

Asked

Viewed 153 times

1

I wrote a code taken from a book but understood that it is the first method to be put, but as there was no example given, I tried to insert it several times in different points. As in this example I have private and public classes I want to know how to get it running:

import javax.swing.JOptionPane;

public class Pessoa {

    private int idade;
    private String nome;

    public void setNome(String nome) {
        this.nome = nome;
            }
    public void setIdade(int idade)  {
        this.idade = idade;
    }
    public int getIdade(){
        return(this.idade);
    }
    public String getNome() {
        return(this.nome);
    }
    public static void ShowInfo(int idade, String nome) {
        JOptionPane.showMessageDialog(null,"Nome:  "+nome +"\nIdade:"+idade, "\nOK", JOptionPane.INFORMATION_MESSAGE);
    }
}

There is a data that I should tidy up, because it continues saying that it has no main method.

4 answers

2

It is necessary to call your code inside the main method, because it is based on it that your application will start running your program, example:

public static void main(String[] args) {        
    Pessoa.ShowInfo(22, "João");
}

Note that as your Showinfo method is static, we don’t need to instantiate the person class.

Look at this question detailing static methods.

1

The main method should be called main and have the following signature (i.e., obey the following standard):

public static void main(String[] args) {
    ...
}

0

The main method is where your application will start running, the starting point, without this method your application will not know where to start.

public static void main(String[] args) {

    /*Seu código vai aqui, todas as instâncias e chamadas de outros métodos são buscadas apartir do método main*/

}

it is within it that your application will be able to call all other features, if there is no need to deploy this method

0

When trying to execute a method, you have to put it inside a 'main'. As you defined the function as 'Static', it is not necessary to instantiate the class to use it:

public static void main(String []args){
    Pessoa.ShowInfo(46, "Rick"); //assim que deve chamá-la
}

Maybe your problem is when trying to run the Pessoa CLASS (I already had this problem when migrating from Python to Java). In this case, try not to run classes without 'main' (otherwise it won’t work).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.