• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

SCJP Brainteaser (5)

 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, the next one is easy... Of course, nobody is supposed to write code like that, but it's very likely that one day somewhere you'll get to maintain poorly written code. So it doesn't hurt starting right now

What happens when the following code is compiled and run?


As usual, you should try to compile and run the code mentally without using any compilers and JVM.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Output should be "Hello"

Because in the Constructor there is no way that the compiler can distinguish that we are talking about the class member.

So it would assign "Hello World" = "Hello World" i.e. String = String.

And as String is initialized to be "Hello" in its declaration itself.
toString() method will use it as it is and will display "Hello" as the output.

Loveen..
Preparing for SCJP1.5
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello

tx
tuhin
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output should be Hello.

There is no problem regarding using String as variable name, since the String data member we are using is in current NameSpace and the String class is in java.lang.String NameSpace.

and for the constructor it will assign the parameter value to itself.

to assign the parameter value to data member we must use this.String = String.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

Shading and:
There are no reserved keywords that start with an uppercase letter.
"instance" is also no reserved keyword.


Bu.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


and for the constructor it will assign the parameter value to itself.
[/QB]



Bingo - That's the trick part of the question right there (if you can call it a trick of course).
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Though the program has variable shading in main method(by declaring String)but all the trick lies in the constructor,assigning the instance varilable values in its own.
So the output is Hello.
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alright, Good job! Congratulations on spotting that shading thing!

Relevant pointers to the JLS:
8.3.3.2 Example: Hiding of Instance Variables

Thanks to those who participated. Stay tuned...
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Valentin,

Waiting for the next one???
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
change the line:
String=String
to
this.String=String
the O/P comes "Hello World"
this is because in String=String the compiler takes both the String as local varialble of that function and overwrites the value of Local String variable with the same value...
when local variable and class variable has the same name and "this" is not used to distinguish b/w local and class variable the compiler gives preference to local variable
[ November 07, 2006: Message edited by: Priyam Srivastava ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic