Why void restriction is there to main() in java

What if i want to return a static value without creating an instance

//return TestStatic.getit();see code below

What will happen to the value returned by this TestStatic.getit(); see code below

public class TestStatic {

private static int x=0;

public static int getit()

{

return x;

}

public static void main(String[] args) {

//System.out.println(TestStatic.getit());

//return TestStatic.getit();

TestStatic.getit();

}

}

[512 byte] By [Z_skm76a] at [2007-11-27 3:20:19]
# 1
you can't return an int in a void method
calvino_inda at 2007-7-12 8:23:01 > top of Java-index,Java Essentials,Java Programming...
# 2

In the future, please use [ code ] tags (see that button between "Spell Check" and "Quote Original").

> What will happen to the value returned by this TestStatic.getit(); see code below

> TestStatic.getit();

Nothing will happen to it. The method will return an int containing the same value as x, but it is never stored in any variable so it is discarded.

Lokoa at 2007-7-12 8:23:01 > top of Java-index,Java Essentials,Java Programming...
# 3

Calvino

i think You didn't get my question . i know that it cannot return an int

but why its return type is void .

read the answer by LOKO , he has explained it correctly .

one more reason which i found is that , since main is static it will be created before main and it will have no idea about any method returning any value which is going to be created after main.

2nd point is that we are not expected to call /return an value using a static method (see my example code above ) as java is purely an object oriented language and it assumes that to do something an object must be there . that is the reason the void restriction is there

Message was edited by:

Z_skm76

Message was edited by:

Z_skm76

Z_skm76a at 2007-7-12 8:23:01 > top of Java-index,Java Essentials,Java Programming...
# 4

> Calvino

> i think You didn't get my question . i know that it

> cannot return an int

your return statement, even if into comment was ambiguous, then

> but why its return type is void .

>

> read the answer by LOKO , he has explained it

> correctly .

>

> one more reason which i found is that , since main is

> static it will be created before main and it will

> have no idea about any method returning any value

> which is going to be created after main.

>

> 2nd point is that we are not expected to call

> /return an value using a static method as java is

> purely an object oriented language and it assumes

> that to do something an object must be there . that

> is the reason the void restriction is there

>

static methods may return values... just one example over thousands of other: Integer.parseInt() returns an int value

java being a object oriented language do not mean that static methods should be void ones everytime

plus: main is a particular case, so it has to be seen differently from other methods

> Message was edited by:

> Z_skm76

calvino_inda at 2007-7-12 8:23:01 > top of Java-index,Java Essentials,Java Programming...
# 5
> plus: main is a particular case, so it has to be seen> differently from other methodsWhy? What's so special about main? It's just a normal static method. Kaj
kajbja at 2007-7-12 8:23:01 > top of Java-index,Java Essentials,Java Programming...
# 6
the main constitutes an entrypoint to the application, what other static methods do not do
calvino_inda at 2007-7-12 8:23:01 > top of Java-index,Java Essentials,Java Programming...
# 7
> the main constitutes an entrypoint to the> application, what other static methods do not doOnly because the JLS defines it that way. Other than that - a special use for a method with that signature - it's still just a normal method.
CeciNEstPasUnProgrammeura at 2007-7-12 8:23:01 > top of Java-index,Java Essentials,Java Programming...
# 8
true.
calvino_inda at 2007-7-12 8:23:01 > top of Java-index,Java Essentials,Java Programming...