1

This does not work

public String getjLabel4text(void){...

But this does

public String getjLabel4text(){...

Why so? I am not taking any arguments so shouldn't I write void there? Why is it causing an error?

4
  • 3
    Ehm. Yes, that's because of the syntax. Commented Apr 8, 2014 at 12:26
  • 6
    It's Java, not C++ (side note, it's a method, not a function). Commented Apr 8, 2014 at 12:26
  • 2
    Because Java is not C. Commented Apr 8, 2014 at 12:26
  • By the way, in OOP, one call it a method, not a function. Commented Apr 8, 2014 at 12:26

6 Answers 6

3

More generally, method declarations have six components, in order:

Modifiers—such as public, private, etc.

The return type—the data type of the value returned by the method, or void if the method does not return a value.

The method name—the rules for field names apply to method names as well, but the convention is a little different.

The parameter list in parenthesis—a comma-delimited list of input parameters, preceded by their data types, enclosed by parentheses, (). If there are no parameters, you must use empty parentheses.

An exception list—to be discussed later.

The method body, enclosed between braces—the method's code, including the declaration of local variables, goes here.

Oracle Documentation

Sign up to request clarification or add additional context in comments.

Comments

2

Java is not like C/C++ You do not need to put void parameter on a function with no parameters, and it throws an error because it is not a valid parameter definition as in java you can not have parameters of type void.

Comments

2

In C and C++ we can take void as an method argument because void is a datatype in both c and c++ . But Java is different, Here void is not a datatype rather its a keyword that is used just to signify method will not return any argument. so taking void as argument is technically invalid and is of no use, this () itself is sufficient.

Ex.

public String Name() { return "Hello";}

and

public String Name(void){ return "Hello";}

In C : Name() will take unspecified no of argument for unspecified type and Name(void) will take no argument.

In C++ : Both Name() and Name(void) will take no argument.

In Java : Name() will work while Name(void) will return compilation error.

Comments

1

There is no need to explicitly mark method as no argument in Java. You just omit any argument definition and there it is, a method with no arguments. There is, though a need for return type for any method. It's where void gets in.

Comments

0

void is just for specifying a return value. Declaration of a return type is required for the syntax to work correctly. Since the argument list length is variable, having none is acceptable. void is a keyword, and is not allowed in the argument list.

Comments

-1

A method can return a value or not.

public void doSomthing(String text) {
   print text;
}

This don't return any value but executes and returns.

public String doSomthing(String text) {
   return text;
}

This method returns the text.

You can't input a void.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.