• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??"
}
}
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was atleast expectting a runtime exception as array a not initialised
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1077
Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes true sorry how silly of me RunTime Exception comes before compilation
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic