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]

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 >

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
> 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