When I try to compile this:
import java.awt.* ;
class obj
{
public static void printPoint (Point p)
{
System.out.println ("(" + p.x + ", " + p.y + ")");
}
public static void main (String[]arg)
{
Point blank = new Point (3,4) ;
System.out.println (printPoint (blank)) ;
}
}
I get this error:
obj.java:12: 'void' type not allowed here
System.out.println (printPoint (blank)) ;
^
1 error
I don't really know how to start asking about this other than to ask:
When we run the code, the output throws an error, void type not allowed here . It happens because printMessage1() already has a print statement that prints the value , and it doesn't return anything when we call the function in a print statement; there's nothing to print in the main method.
Definition and Usage The void keyword specifies that a method should not have a return value.
A void return type simply means nothing is returned. System. out. println does not return anything as it simply prints out the string passed to it as a parameter.
It is a keyword and is used to specify that a method doesn't return anything. As the main() method doesn't return anything, its return type is void. As soon as the main() method terminates, the java program terminates too.
If a method returns void
then there is nothing to print, hence this error message. Since printPoint already prints data to the console, you should just call it directly:
printPoint (blank);
You are trying to print the result of printPoint
which doesn't return anything. You will need to change your code to do either of these two things:
class obj
{
public static void printPoint (Point p)
{
System.out.println ("(" + p.x + ", " + p.y + ")");
}
public static void main (String[]arg)
{
Point blank = new Point (3,4) ;
printPoint (blank) ;
}
}
or this:
class obj
{
public static String printPoint (Point p)
{
return "(" + p.x + ", " + p.y + ")";
}
public static void main (String[]arg)
{
Point blank = new Point (3,4) ;
System.out.println (printPoint (blank)) ;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With