Results 1 to 13 of 13
  1. #1
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default About precedence of methods

    Hi everybody, please help me I'm confuse about the precedence of methods, I need a sample of methods with its body.

    How can you prove that a method will read from left to right, example:
    Java Code:
    String a = method1().method2().method3();
    //How does the method1() passes the value to method2() and the method2() passes the value to method3()?
    Last edited by RichersooN; 06-22-2011 at 07:20 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What's your thought process on this proof?

  3. #3
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    What's your thought process on this proof?
    I dont know how the multiple methods in a single variable works like my example
    Java Code:
    String a = method1().method2().method3();

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I'd like to see what you think and help you rather than giving a direct answer, try using some java class methods(perhaps string) and change the ordering around and see how stuff changes.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    By "precedence" do you mean you are unsure of which order the methods will be called?

    In that case write implementations for the methods which includes a line which outputs the method name. Post code and question if you can't understand the output.

  6. #6
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    I'm new to Java, I don't know how the multiple methods works and whats the method's prototype. Can you example cause your an excellent Java programmer.

  7. #7
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    By "precedence" do you mean you are unsure of which order the methods will be called?

    In that case write implementations for the methods which includes a line which outputs the method name. Post code and question if you can't understand the output.
    I'm not sure how this works, its method's prototype and how the methods1() pass the value to method2(). The only thing I knew is it's from left to right.
    Last edited by RichersooN; 06-22-2011 at 07:18 AM.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    Sheesh!
    Java Code:
    class Foo {
        public Foo methodOne() {
            System.out.println("Method One");
            return this;
        }
    
        public Foo methodTwo() {
            System.out.println("Method Two");
            return this;
        }
    
        public Foo methodThree() {
            System.out.println("Method Three");
            return this;
        }
    
        public static void main(String[] args) {
            new Foo().methodOne().methodTwo().methodThree();
        }
    }
    Not that difficult. All it took was exercising the grey matter a liitle.

  9. #9
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    Sheesh!
    Java Code:
    class Foo {
        public Foo methodOne() {
            System.out.println("Method One");
            return this;
        }
    
        public Foo methodTwo() {
            System.out.println("Method Two");
            return this;
        }
    
        public Foo methodThree() {
            System.out.println("Method Three");
            return this;
        }
    
        public static void main(String[] args) {
            new Foo().methodOne().methodTwo().methodThree();
        }
    }
    Not that difficult. All it took was exercising the grey matter a liitle.
    tHANKS,
    What if the method3() has a prototype of this
    Java Code:
     public Foo methodThree(int x, int y) {
            System.out.println("Method Three");
            return this;
        }

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    So what if it did? The only change you would have to make is pass the two parameters.

  11. #11
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    So what if it did? The only change you would have to make is pass the two parameters.
    Where's that two parameters coming from? From the two previous methods???

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,785
    Rep Power
    7

    Default

    Huh?

    Do you know how to pass parameters to methods? This concept has nothing to do with chaining method calls or order of precedence.

  13. #13
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 07:50 AM
  3. Replies: 4
    Last Post: 04-03-2009, 06:36 AM
  4. Get and Set Methods
    By kian_hong2000 in forum New To Java
    Replies: 10
    Last Post: 08-27-2008, 02:06 PM
  5. Methods
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 08:49 PM

Posting Permissions

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