• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question of the day from marcus green

 
vinayak manda
Ranch Hand
Posts: 31
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

I promise, I haven't touched a drop today...
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barry Gaunt:
So I reckon the local a in main is not really shadowing the a which is a class member.


I think you are wrong.

Let's take a look at the definition of shadowing:

from http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#34133

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.


Now let's take a look at "scope":

from http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#103228

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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK I bow to that, you can can drag me away rantin' and ravin' now.
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic