Helpful Information
 
 
Category: Java
accessModifier, returnType, returnValue

Can anyone explain to me how accessModifier, returnType, and returnValue work? and also how does "return" work also?

thanks for any help

private int returnFive() {
return 5;
}private is an example of an access modifier. It means that only code within that class can access the function. Others exist: public will allow anybody to access the function/variable, while protected will allow code in that class or any class that extends that class to access the function/variable. The green int declares that this function will return an int. This is necessary because Java is a strongly- and statically-typed language, so the compiler must know the types of any potential data in advance. return is the value returned by the function when called; this must be of the type declared as the function's return value. If we were to do, in some other code:
int five = returnFive();... the int five would hold the value 5.

The returnType and returnValue must be the same type right?

Return type and return value, please. They're not keywords, you don't need to put them in camelCase :p

Yes.

Ok thanks.
can the access modifier be left out? or will i get some type of error? also what do the the access modifiers specifically mean?

Yes, it will default to protected.


private is an example of an access modifier. It means that only code within that class can access the function. Others exist: public will allow anybody to access the function/variable, while protected will allow code in that class or any class that extends that class to access the function/variable.

thanks. i knew i had seen it somewhere. sorry to ask again.

THANKS










privacy (GDPR)