5 Replies - 414 Views - Last Post: 22 August 2013 - 12:35 PM Rate Topic: -----

#1 M.ALvi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-March 13

function of PSVM

Posted 20 August 2013 - 10:37 PM

i wanted to ask about public static void main(String[]args)
when to use this and when to use such as public ......any thing in function ??
Is This A Good Question/Topic? 0
  • +

Replies To: function of PSVM

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7644
  • View blog
  • Posts: 12,890
  • Joined: 19-March 11

Re: function of PSVM

Posted 20 August 2013 - 11:07 PM

I don't think I understand what it is you're asking, but you use the main method when you want to be able to start a program by calling this class. The main method is called when the jvm is created, for example by runniing the following command:

% java Foo


where there exists a file Foo.class which is the result of compiling a source file Foo.java.

From the jvm spec:

Quote

The Java virtual machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the boostrap class loader. The Java virtual machine then lnks the initial class, initializes it, and invokes its public class method void main (String[]). The invocation of this method then drives all further execution.


That is, when you invoke the jvm specifying the class Foo as the one you'd like to run, the jvm does some magic with the .class file and then calls the main method. Everything that happens after that is the main method's fault.
Was This Post Helpful? 1
  • +
  • -

#3 M.ALvi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 18-March 13

Re: function of PSVM

Posted 22 August 2013 - 12:04 PM

yes but why is it i see some programs with main method such as public static int[] merge_sort(int[] m){???? and when to use void main and how exactly much there are methods to use ????
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7644
  • View blog
  • Posts: 12,890
  • Joined: 19-March 11

Re: function of PSVM

Posted 22 August 2013 - 12:14 PM

That wouldn't be a main method. A main method is a method with the signature
public static void main (String[])



main is used just in exactly one circumstance: when you'd like to be able to start up an instance of the jvm and have it start running a program. All other methods are used to provide access to an object's functionality (or a class's, in the case of static methods) so that other objects can make requests of it.

For example, your merge_sort method would provide objects with a way to submit an array of ints m and receive back an array of ints, call it n such n contains all of the elements of m, but arranged according to their natural ordering. Since these are ints, that's just what we call "numeric order". (A more generalized merge sort would be able to accept arrays containing any object, as long as it had a natural ordering as defined by a compareTo method)

I think it might be good if you were to go through these tutorials and then come back with your questions.
Was This Post Helpful? 0
  • +
  • -

#5 axel1994  Icon User is offline

  • D.I.C Head

Reputation: 83
  • View blog
  • Posts: 167
  • Joined: 27-May 13

Re: function of PSVM

Posted 22 August 2013 - 12:19 PM

There is only one main method, and that is :
public static void main(String[] args){}

public static int[] merge_sort(int[] m){}
is just a method.


I think that you think that because it says: public static something something(something[] something){}
it makes it a main method.
This isn't true.

public/static are just keywords.
I would read into them.
I think that if I explain it now, it'll be too complicated.

The int[] or void means that you return a int[] (an array of integers) or nothing.
public int something(){}
will return a number, void won't return anything.

the (int[] m) just means that the method needs an array of integers or else it won't work.
Like when having a method:
public void something(int a){}
This means that when I want to use the method something I need to call it as following:
something(B);
where b is a number.(integer)

Note: not every java file needs a main method.
Was This Post Helpful? 2
  • +
  • -

#6 axel1994  Icon User is offline

  • D.I.C Head

Reputation: 83
  • View blog
  • Posts: 167
  • Joined: 27-May 13

Re: function of PSVM

Posted 22 August 2013 - 12:35 PM

My reply is perhaps kind of confusing:

Let me explain again:

We have a function called:
public static int cube(int x){
    return x*x;
}



Let's analyse this function.

When we want to use this function we need to pass an integer to the function.
We can't calculate the cube of nothing.
So we need an integer:
 
int x = 5;
int xcube = cube(x);

The following won't work:
int y = cube();



Next up we got the int that comes after static.
This means that the function returns an integer.
If it would say "void", then xcube wouldn't be anything, since cube(x) wouldn't give anything back.

Now the keyword "public":
This means that anyone can acces this function.
Right now, this might seem weird.

But when you'll read into OOP (object oriented programming) it will.
The easiest way to explain:
Have you ever used Math.something in Java?

Math is also a java file with methods.
When we use Math.something, we are working in another file.
For example: firstjava.java
So every method we want to use from Math, needs to be public.
Else, we won't even know it exists.

Now "static":
Lets continue with the Mathclass.

We could say:
Math ma = new Math();

This means we now have a variable called ma, and we can change this variable (perhaps).
(change:

Lets say we want to simulate a spaceship,
we have a spaceship class which have their own x and y values

When we say:
Space ship = new Space();
We create a ship, but this ship is not connected to:
Space ship2 = new Space();

Its like have 2 numbers, but number1 = 5 and number2 = 6. So they aren't equal.
)
Or we can call methods from it.

But if we call Math.cube(x); we want to get the same result no matter if it's from ma
or from:
Math ma2 = new Math();

Then we call that method (cube(x)) static.

If the method changes depending on the situation (different results from ma and ma2) then we don't call it static.

Kind of a complex explenation, but once you'll read into OOP (object oriented programming), you'll understand better.

This:
public static void main (String[] args)

is the only main method.

public static void main2 (String[] args)

^ that isn't a main method

It needs to be exactly the same.

I would recommend buying a book on java and reading (and doing exercises) from coves to cover.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1