aspose file tools*
The moose likes Beginning Java and the fly likes why is the  System.out.println(); line displaying error  when i type void method in it? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "why is the  System.out.println(); line displaying error  when i type void method in it?" Watch "why is the  System.out.println(); line displaying error  when i type void method in it?" New topic
Author

why is the System.out.println(); line displaying error when i type void method in it?

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

class Demo
{
public static void display()
{
int a[];
}
public static void main(String args[])
{
System.out.println(display()); //"error:void notallowed here why is that so??"
}
}


http://www.lifesbizzare.blogspot.com || OCJP:81%
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1760
    
    7

A method declared void doesn't return any kind of value, so it's not a valid argument to the println() method, or any other method for that matter.
What would you expect this code fragment to do?


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

i was atleast expectting a runtime exception as array a not initialised
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1760
    
    7

You'd need to get the code to compile before ever being able to see a RuntimeException, wouldn't you
As for the initialization of the local variable "a", you are correct to say that it never will be initialized, but it doesn't matter, because it is never used.
As soon as you try to access the variable "a" inside the display() method, the compiler will let you know that variable "a" still needs to be initialized.
Try it.
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

yes true sorry how silly of me RunTime Exception comes before compilation
 
 
subject: why is the System.out.println(); line displaying error when i type void method in it?