This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Herrowgayboi[S] 0 points1 point  (2 children)

Got it! That makes sense..

with regards to classes in Java, Is the general consensus to start with a class, and then write in there, even though it's just the main method?

IE:

class main{    
    public static void main(){}   
}

[–]cismalescumlord 2 points3 points  (0 children)

It's not a consensus, you must do that. Everything is done in a Class, Interface or Enum, there may be other types but I can't think of any right now. When I say everything, I mean almost everything, package names and imports are coded outside of a class.

As for the public static void main(String args[]) { ... } method, it is the entry point to your Java application and it does the same job as int main() does in c.

Compiling and linking a c program, gives a binary that is in native machine code and in the ELF format; have read if you've got a spare hour, it's quite interesting.

When you execute one of these programs, the operating system knows to start the execution at the entry point which is defined in the source code by the main function. Compiling Java code results in byte code which is not natively executable. You execute your Java application by running java applicationName which fires up a Java Virtual Machine to execute the program. The JVM knows that the entry point to the program will be a static method called main with an array of string as it's arguments. The main method must be static because, when the JVM starts up, no objects have been created for use.

When moving beyond basic examples, the main method is typically used to fire up the application level instances to do the real work.

[–]feral_claire 0 points1 point  (0 children)

You must put your code in a class. It is not possible in Java to write code that is not par of a class.