Results 1 to 7 of 7

Thread: accessModifier, returnType, returnValue

  1. #1
    Join Date
    Jan 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Code:
    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:
    Code:
    int five = returnFive();
    ... the int five would hold the value 5.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Jan 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    The returnType and returnValue must be the same type right?

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

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

    Yes.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #5
    Join Date
    Jan 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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?

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Yes, it will default to protected.

    Quote Originally Posted by myself
    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Jan 2007
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

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

    THANKS

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •