开发者

Having a little trouble with this Java code. Beginner probably mixing some things with C#

package practico1;
/**
 * Programador: Sergio Tapia Gutierrez
 * Fecha:       Lunes 10, Mayo - 2010
 * Practico:    1
 */
public class Main {    
    public static void main(String[] args) {
        System.out.println("Esta es una pequena aplicacion para mostrar los");
        System.out.println("distintos tipos de datos que existen en Java 6.");

        //boolean, char, byte, short, int, long, float, double, String

        ejemplosBoolean();
    }

    public void ejemplosBoolean(){

    }

}

So, I'm just testing some things out, but I'm getting an error claiming that I'm trying to run ejemplosBoolean() in a static context when it isn't a static method.

My question is, in Java do methods have to have static in order to use them even if they are in 开发者_JAVA百科the same class?


Sergio, you are running a non static method within a static method, if you want ejemplosBoolean() make it public static void ejemplosBoolean(). Cheers Saludos desde Mexico


In your main, you need to say something like:

Main m = new Main();
m.ejemplosBoolean()


You must create an instance of the class Main to call the ejemplosBoolean method. You are trying to call it from the main method. Try something like this.

public class Main {    
public static void main(String[] args) {
    System.out.println("Esta es una pequena aplicacion para mostrar los");
    System.out.println("distintos tipos de datos que existen en Java 6.");

    //boolean, char, byte, short, int, long, float, double, String
    Main m = new Main();

    m.ejemplosBoolean();
}

public void ejemplosBoolean(){

}

}

Also, I would probably name my class something other than main to keep the confusion to a minimum.


The problem is that your main application code is in a static method (main) and so when the code is executing there there is no instance of Main class to use on which to execute ejemplosBoolean().

This is similar to what you would have in C#:

public class Program {
    public static void Main() {
        Console.WriteLine("Esta es una pequena aplicacion para mostrar los");
        Console.WriteLine("distintos tipos de datos que existen en Java 6.");

        ejemplosBoolean();
    }
    public void ejemplosBoolean() {
    }
}

Similar error would happen, no instance of Program class on which to call ejemplosBoolean().


If you don't want to make methods static, but still want to call them from your public static void main, you'll have to make an instance to call them on:

Main m = new Main();
m.ejemplosBoolean();

and so on (you can then reuse that m for other methods you may want to call, since it has no state, at least with the code you've shown;-). It wouldn't be different in C# though!


The behavior you're seeing is consistent with C#. main is a static method, which means that you can only call other static functions from within it unless you have an object reference.

If you wish to call ejemplosBoolean, you will need to mark it as static as well or initialize a new instance of the Main class and call it on that instance.


Your ejemplosBoolean() is a non-static method, so it would require an instance of class Main to call that method upon. The method must be declared static to call it from the static context without creating an instance first. This is entirely consistent with C#.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜