This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question of the day from marcus green Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question of the day from marcus green " Watch "Question of the day from marcus green " New topic
Author

Question of the day from marcus green

vinayak manda
Ranch Hand

Joined: Jul 27, 2004
Posts: 31
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

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
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
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

Joined: Jul 11, 2001
Posts: 14112
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.


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
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
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Question of the day from marcus green
 
Similar Threads
Result of the Code
Please help me with this question!
Thread Question -JQ++
toArray(Object [] a) ?
Multidimensional arrays