Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Static Method Help Please?

public static String sayAgain( String word, int num )

{

int k;

String result = "";

for ( k = 0 ; k < num ; k++ )

{

result += word;

result += " ";

}

return result;

}

public static void Laugh()

{

String str = "ha";

System.out.println( sayAgain( sayAgain( str, 2 ), 3 ) );

}

Which of the following is output as a result of evaluating Laugh()?

ha

ha ha

ha ha ha

ha ha ha ha ha ha

None of the above — an error will occur.

2 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    You again...

    You know you could actually try this one out.

    Just add in a a main method, and I think you're set to compile and run. Tsk tsk tsk...

    as so...

    public class crap

    {

    public static String sayAgain( String word, int num )

    {

    int k;

    String result = "";

    for ( k = 0 ; k < num ; k++ )

    {

    result += word;

    result += " ";

    }

    return result;

    }

    public static void main(String[] args)

    {

    String str = "ha";

    System.out.println( sayAgain( sayAgain( str, 2 ), 3 ) );

    }

    }

    First method ran is the inner one (( sayAgain( str, 2 )), then it goes outward....

    Basically the string is ran through a for loop than runs under whatever int num is (the 1st time it runs only 2 times, right?)

    So it'll print the string 2 times, once for everytime the for loop is running.

    Then the second method sayAgain( sayAgain( str, 2 ),3

    is run. The for loops runs 3 times now, printing the result of the first method 3 times, 3 times 2 = 6.

    6 instances of the string "ha".

    I probably left out some things ... ON PURPOSE. Go ask a classmate about these things, or maybe your teacher/ a tutor.

    GL in class!!! Thanks for letting me practice my knowledge.

  • Anonymous
    5 years ago

    static is a 'modifier'. We use modifiers all the time in English. We don't even think about them: I live in a house. (Most of us do). I live in a bird house. (bird is a modifier that immediately tells you that I'm a bit odd...) Same thing with static: foo is a method of class bar. foo is a static method of class bar. If foo is a method of bar, then I would assume to call foo, I would need to create an instance of bar first: bar b = new bar(); b.foo(); If foo is a *static* method of bar, then I would know that would *not* need to create an instance of bar: bar.foo(); HTH

Still have questions? Get your answers by asking now.