Jump to content
  • Advertisement
Sign in to follow this  
Xer0botXer0

Println works in one class but not another

This topic is 405 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement

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

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


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


Link to post
Share on other sites

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 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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!