• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Bear Bibeault
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • salvin francis
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
Bartenders:
  • Jj Roberts
  • Carey Brown
  • Scott Selikoff

Static Variables Shadowing : Q from Mock

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Found this question on a mock:
Question 38.
Given the code, what is the output

A.Code Does't Compile.
B.Code Compiles but error will be generated at runtime.
C.Code Compiles & runs, output is
i is : 10
D.Code compiles & runs, output is
i is : 20
Ans is A. The code does not compile.
However if I replace line line 4 with
int i = 20; it compiles fine and runs with output "i is 20".
Also if I make both Line 2 and Line 4 non-static, it compiles and runs with output "i is 20".
Lastly if I make Line 2 non static, and 4 static it again fails to compile.
Am I right in saying that:
1. Static variables cannot be shadowed by another static variable. But it can be shadowed by a non-static variable.
2. Non Static variables can be shadowed by another non-static variable, but not by a static variable.
Monisha.
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually... the problem is that you can't declare a local variable as static.
�14.4 Local Variable Declaration Statements
[ April 10, 2003: Message edited by: Jessica Sant ]
 
Monisha Talwar
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh Yeah ... That's right
Thanks.
 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I bet if you only comment out the 4th line then it will compile. Also, static variables can only be declared as instance variables.
Thanks,
Deep
 
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic