Sign in to follow this  
Xer0botXer0

Println works in one class but not another

Recommended Posts

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 this post


Link to post
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 this post


Link to post
Share on other sites

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 this post


Link to post
Share on other sites

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 this post


Link to post
Share on other sites

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 this post


Link to post
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.

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this