can somebody explain to me what each syntax in the subject title means?
Printable View
can somebody explain to me what each syntax in the subject title means?
public
Self-explantory
static
basically put, this procedure is made accessible from anywhere within your program with this keyword. Main has to be static for the JVM to init your app.
void
doesn't return anything
Main
procedure name
(String [] args)
requires a String array parameter.
in the Main procedure, it would be your command line (JVM handles the passing)
i don't get the static part.........
doesn't public make the procedure accesible? then why static?
public is a visibility keyword - makes the procedure/variable etc accessible from within the program. when used with static, it can be used pretty much anywhere.
take the Math class for example, most of the operations are public static, so u don't have to import the Math class - just reference them e.g. Math.random()
sorry if my explanations are a tad obfuscated - never been good at expressing my thoughts
ok, hm..............visibility eh?
i'm still a bit blurry on this.........
ok, could u at least tell me the advantages and disadvantages of public and static...
Access modifiers alter the visibility of a method or variable. or where it can be used.
A method or variable can have one of 3 different access modifiers. These are public, private and protected (which i wont go into as it's only useful when you get into advanced stuff like inheritance).
public
This means that the method or variable can be accessed from outside the class and inside the class.
private
This means that the method or variable can only be used from inside the class.
the private keyword is normally used when you want to have a variable (or method) that nobody else can modify. You may then provide public methods that allow them to modify it. This is useful if you want to do some other tasks whenever the variable is modified.
static
a static method or variable can be used without having to create an instance of a class.
So in the example of
public static void main(String[] args)
the method is public so it can be called without being inside the class, it is also static so you don't have to have an instance of the class to call it.
The method is used to start a program.
So your starting from nothing. As you have nothing you have to call a method that doesn't need an instance of a class, so it has to be static.
You can't call a private method as again you have no instance of the class to call it from within, so it has to be public.
Hope that helps a bit.
You think to yourself ok I think i can explain that and then you start trying to do so and it becomes a big mess :)