# Println works in one class but not another

## Recommended Posts

Xer0botXer0    120

Hi guys,

So I'm messing around with Java trying to get a feel for how things work,

It's really confusing when a line of code doesn't tell me what it is.

package myPackage;

public class Calculators {

static Integer val1 = 5;
static Integer val2 = 5;

public static void main(String[] args) { //method

Integer val3 = val1 * val2;
System.out.println(val3);
}
}

So what is this main here ? is it a class or a method ?

If it's a class, why cant I execute the println code in the Calculators class ?

if it's a method, why is it being called a main class all over the web. :/ Or rather, if main is a method, then does every class need a main method ? or what needs it ?

##### Share on other sites

Main is a Function / Method ... the Main function / method is what executes and runs when you run an application, all code inside of it is what executes.

A class shouldnt have a Main function in it, a class should have functions / methods that do something... and that class should then be instantiated and the methods executes from within the Main method.

So as a basic guidline... you define classes outside of the Main .. and you create instances of that class inside of the Main

##### Share on other sites
kolrabi    1572

In your code main is a static method the Calculators class. It defines the entry point where code execution begins on program launch (possibly after some initialization of other things). The term "main class" refers to the class that has the static main method. There should be only one such class in the program. If there are more, most likely the one you don't want is being called.

Your code looks fine and should work otherwise.

##### Share on other sites
Xer0botXer0    120

Thanks,  I thought it was a main class that I needed, meanwhile it's a main method.

I don't quite understand what you mean by a class shouldn't have a main function/method in it, because a function/method cant be outside of a class can it ?

I think that sometimes a person wants multiple main methods, why I don't know. I'm going to stick to one until I ever know why.

Just one more important thing, if I create multiple classes and methods and such, I can code them and then when I need each one I just call it in the main method ? that's pretty much how I get a program to run ?

##### Share on other sites
kolrabi    1572

I don't quite understand what you mean by a class shouldn't have a main function/method in it, because a function/method cant be outside of a class can it ?

I didn't say that you shouldn't. In fact you need at least one class with a main method, so that the program can be run. But you should not create more than one.

Just one more important thing, if I create multiple classes and methods and such, I can code them and then when I need each one I just call it in the main method ? that's pretty much how I get a program to run ?

Yes, that's how programs are written in Java.

##### Share on other sites
Xer0botXer0    120

A class shouldnt have a Main function in it

Gibbon said that.

thanks, looks like I'm getting closer to being able to write something basic.

##### Share on other sites

Yeah sorry about that ;o slipped my mind that in Java the Main method is inside a class, but should only exist in one class. Been a few months since I did any real Java work :o

##### Share on other sites
Xer0botXer0    120

Aren't all methods inside of classes ?

Yes apparently a person should only have one main method, but I've read that a person can have multiple.

##### Share on other sites

In Java im pretty sure all methods do have to belong to a class, in languages like C++ you can have functions outside of a class pretty happily.

And yes, you can technically have multiple main functions in Java, which you can use to execute specific code / tests if you want to check parts of a program work.