views:

69

answers:

7
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 the same class?

A: 

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#.

Tetsujin no Oni
The C# bit is wrong. If they are in the same class you can just call on it from wherever in the class.
Serg
@Sergio: You're incorrect. From within a static function, you can only call *other* static functions in that class unless you have an object reference.
Adam Robinson
I'm not talking about a static on static; I'm talking about a good ol public void method, you don't need to create an instance of the parents class to call the method, you just call it if you are within the same class.
Serg
@Sergio: You're simply wrong; what you're describing doesn't even make *sense*. If this were possible, on what instance of the class would this "good ol public void method" operate? It's an instance method, so it *has* to be associated with an instance.
Adam Robinson
+3  A: 

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

JaSk
Gracias viejo, saludos desde Bolivia!
Serg
A: 

In your main, you need to say something like:

Main m = new Main();
m.ejemplosBoolean()
arcticpenguin
Woah, talk about over engineering. :D
Serg
@Sergio: That's not over-engineering, that's the only way to call the function as you've defined it. The same goes for C#.
Adam Robinson
In C# you can call on public methods WITHOUT the static bit. Any method that's in your class is reachable so I don't know what you're talking about.
Serg
@Sergio: Not without an object reference you can't. If you're within an instance method, then yes, you can do that, since you have an object reference (`this`). You cannot do what you're describing in C# any more than you can do it in Java. The code in your question is perfect valid C# code (other than the fact that `System.out.println` isn't a valid function and the fact that you're calling an instance method from within a static method without an object reference). Try to paste that class into a C# project and see what you get.
Adam Robinson
with the advice this kid is getting his programs will be a bunch of static methods inside a global 'main'
arcticpenguin
A: 

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.

Rob Goodwin
A: 

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().

earlNameless
A: 

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!

Alex Martelli
A: 

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.

Adam Robinson