• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

urgent pls

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the code, what is the output
public class MyClass{
static int i = 10;

public static void main(String[] arg){
static int i = 20;
System.out.println("i is :"+i);
}
}
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
pls explain
 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the local var i declared in main() shadows the class var i. SO the o/p will be 20.
Correct me if i am wrong.
Rashmi
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't have static var in your method.
Like u can't have private,public,protected var in your method.
Local var will be gone after the method ends so there is no need.

[This message has been edited by FEI NG (edited November 07, 2001).]
 
Fei Ng
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh oh.. sorry.
Given answer is A right?
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah...Fei is right...you cannot have static variable declarations in methods. The correct answer is A.
However, Madhu, do take a look at this code...

Hope this helps
Shyam
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I tried to compile this code but it wouldn't compile due to the static variable in the main method. If I remember correctly you can't declare static variables in methods. You can have them as class variables however. If anybody can elaborate or correct me please fire away.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. you can't declare static variables in a method. quit logical though. what one can mean having a staic variable in a method???
Regards,
-Maulin.
 
reply
    Bookmark Topic Watch Topic
  • New Topic