aspose file tools*
The moose likes Associate Certification (OCAJP 7/8) and the fly likes main method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Associate Certification (OCAJP 7/8)
Bookmark "main method" Watch "main method" New topic
Author

main method

Alex Derek
Ranch Hand

Joined: Apr 10, 2013
Posts: 32

Ok, some questions for James (or whoever wants to reply :-) ).

1) can you explain me why in main method there is an array String passed as arguments? I mean, what is it's utility (seen that almost always the String array is not used)?
Wouldn't it be better to create a standard main method with no parameters? Or maybe some overloaded method...

2) where is main method? i've tried to look for it in the docs api, in the index under 'M' there's no such method as main, there is only the following:

MAIN - Static variable in class javax.print.attribute.standard.MediaTray
The main input tray in the printer.
MAIN_CLASS - Static variable in class java.util.jar.Attributes.Name
Name object for Main-Class manifest attribute used for launching applications packaged in JAR files.
MAIN_UIROLE - Static variable in class javax.print.ServiceUIFactory
Denotes a UI which performs the normal end user role.

3) a question about static methods, which aren't still clear to me. Why is it possible to create an instance of a class inside the main method, even if the main method is static? In a static method shouldn't be possible to call only static variables and if not, they should be put as method parameters?
for example

this compile and works (obviously). It's not clear to me, why :-)

Alex


(...)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41838
    
  63
#1) The args array is for passing parameters to the app; this is useful when starting the app from the command line, or from within a script. It would not work if the app is a double-clickable jar file, because there would be no way to enter parameters.

#2) Any class can have a main method, thus making it an entry point to start an app. But no class is required to have such a method, so it's not in the javadocs of any particular class. It's probably defined that way in the JVM specification.

#3) In a static method you can not access instance variables and methods, because they're running outside of any object context. But it is possible, as you have found out, to create objects and assign them to local variables because it's Java code like any other. (Note that there is a difference between an instance of a class -i.e., an object- and an instance variable.)


Ping & DNS - my free Android networking tools app
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5266
    
  13

1. It's utility is that you can pass arguments to your program without any hassle. I don't get why you would want a main method without parameters If you don't need parameters for an application, simply don't pass any when invoking your application and in your code just ignore the String array. But if you need them for your application, it's available out-of-the-box. So this solution has the best of both worlds.

2. The main method is only needed when you have to create an executable class. It's not a class or constant which can be found seperately in the javadoc API. It's a particular part (a static method) of a class. So you'll 1st have to look for an executable class and then you'll find the main method described in the javadoc of this class. But I'll doubt if you'll find any executable class in java API. These classes are considered to be building blocks to let a developer (like you and me) create an application, not to be executed seperately. If you want to know more about the main method, take a look at this lesson.


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5266
    
  13

Consider this example:


If you try to access an instance variable (number) from a static method, you'll get a compiler error. So from a static method you can only access static (class) variables and methods. And the reason is quite simple: a static method can be invoked when no instances of that class are created. So if there is no Example instance created, what would be the value of number? That's impossible to decide, so you'll get a compiler error.
But in a static method you can create instances of a class (ex1 and ex2) and on that instance you can access both static (class) and instance variables (and methods) as the above code snippet demonstrates. You can only access an instance variable through an instance (hence its name "instance variable" ), but that can be in a static method
Alex Derek
Ranch Hand

Joined: Apr 10, 2013
Posts: 32

Wow, thank you very much Roel and Ulf for the exaustive informations and the quick reply!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: main method