Question of the day from marcus green

Excuse me as I'am not sure that I can create new thread abt this topic

Which of the following statements are true of the following code?

1)If it is run with no command line parameter it will show Hello there on the console 2)If it is run with no command line parameter an IndexArrayOutOfBoundsException will be thrown 3)If a command line is passed to the program it will be printed out after the word "Hello" 4)This code will not compile

Answers are 2 & 3. Correct if I'am wrong,as String array declared at class level is instance variable,so a[0] in main has to compulsorily search the arguments at the command line. Then why is this statement "The String array passed to main will shadow the one created at class level" valid.

This example would be more correct if the first String array a was declared as static. Then the reference to the parameter a in the static main method shadowing the first a is correct. The first a, being a member variable, cannot be referenced in the println call because main is static. [ September 24, 2004: Message edited by: Barry Gaunt ]

Can you expand on that Barry? Are you commenting on the statement "The String array passed to main will shadow the one created at class level"

Marcus

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729

posted Sep 24, 2004 10:34:00

0

Hi Marcus, yes, I'm commenting on that "The String array passed to main will shadow the one created at class level". If in the argument list to the static main method you have String[] b instead of String[] a, then the program will not compile because the reference to a[0] is not through an object instance. So I reckon the local a in main is not really shadowing the a which is a class member. To me it seems to be not quite kosher somehow, if you get my meaning.

However, if the class member a is made static, the local a in main does properly shadow it.

On the other hand, a non-static method with String[] a as a parameter, does properly shadow the one defined in the class.

A declaration d of a field, local variable, method parameter, constructor parameter or exception handler parameter named n shadows the declarations of any other fields, local variables, method parameters, constructor parameters or exception handler parameters named n that are in scope at the point where d occurs throughout the scope of d.

The scope of a declaration of a member m declared in or inherited by a class type C is the entire body of C, including any nested type declarations.

So, although the non-static field is not accessible, it *is* in scope, and therefore can be shadowed.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729

posted Oct 03, 2004 04:42:00

0

OK I bow to that, you can can drag me away rantin' and ravin' now.

Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813

posted Oct 03, 2004 11:22:00

0

Ah, now I understand what Barry was concerned about. I believe that the real exam will not split hairs so finely, and I probably ought to create an alternative option for that question.