PDA

View Full Version : accessModifier, returnType, returnValue



craigtb
01-29-2007, 03:17 AM
Can anyone explain to me how accessModifier, returnType, and returnValue work? and also how does "return" work also?

thanks for any help

Twey
01-29-2007, 02:34 PM
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.

craigtb
01-29-2007, 07:57 PM
The returnType and returnValue must be the same type right?

Twey
01-29-2007, 08:16 PM
Return type and return value, please. They're not keywords, you don't need to put them in camelCase :p

Yes.

craigtb
01-29-2007, 10:01 PM
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?

Twey
01-29-2007, 10:23 PM
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.

craigtb
01-30-2007, 12:52 AM
thanks. i knew i had seen it somewhere. sorry to ask again.

THANKS