Issue while calling a method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mota
    New Member
    • Dec 2010
    • 3

    Issue while calling a method

    Code:
    public class Sample 
    {
    	public int called()
    	{
    			System.out.println(".....I shouldn't be here.....");
    		return 0;
    		
    	}
    	
        public void caller()
    	{
    		System.out.println("It is supposed to be here -->(" + called() + ")<-- Why isn't it working?\n");
    	}
    }
    
    
    public class Test
    {
    	public static void main(String args[])
    	{
    		Sample me = new Sample();
    		me.caller();
    	}
    }
    And the output would be really weird.. it will be like this:
    .....I shouldn't be here.....
    It is supposed to be here -->(0)<-- Why isn't it working?

    so why is it like this? is this an error or a bug or is it intended? if it was intended, explane how please..
  • Amit Kumar M
    New Member
    • Nov 2010
    • 9

    #2
    Please read this article which talks about how method call works in java.

    Comment

    • Dheeraj Joshi
      Recognized Expert Top Contributor
      • Jul 2009
      • 1129

      #3
      You are calling called() from caller() method.

      Regards
      Dheeraj Joshi

      Comment

      • mota
        New Member
        • Dec 2010
        • 3

        #4
        Maybe I wasn't clear enough.
        yes I know that I called called() by caller().
        The problem is that the text from called() was written above the text from caller(). While the returned value (0) was placed in the appropriate please.
        I mean shouldn't the text from called() be written inside text from caller()? why is the return value is separated from the text?

        Thanks in advance.

        Comment

        • Dheeraj Joshi
          Recognized Expert Top Contributor
          • Jul 2009
          • 1129

          #5
          No it won't.

          When caller() is called. It tries to print the value. But there is a method called in between and it gets executed (i.e called() method)

          Since called() method has a System.out.prin tln(). It prints that first.

          Then the call is returned to caller method and finally it displays the text.

          Code:
          caller()
          called() --> called being called from caller method
          It does the operation. (Basically prints and returns 0) ans control comes back to caller().

          Regards
          Dheeraj Joshi

          Comment

          • mota
            New Member
            • Dec 2010
            • 3

            #6
            Thank you for enlighten me with your explanation Dheeraj Joshi. It was really getting into my nerves.

            thanks again, really appreciate it!

            Comment

            • Dheeraj Joshi
              Recognized Expert Top Contributor
              • Jul 2009
              • 1129

              #7
              Happy to help.

              Regards
              Dheeraj Joshi

              Comment

              Working...