Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

about static

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

The following qn found in one of the mock exam
what is the output


what is the output?

My view is
its a compiler error
but the ans is "it prints 20"

how it is?

actualy
static{
}
is not a method its a block of code
how the variable i declared with in i is ignored..

tx in adv..
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have not been able to find an explianation.. However I changed the code a bit:



when i try to compile it, it says:
C:\scjp>javac Q1.java
Q1.java:7: cannot resolve symbol
symbol : variable i
location: class Q1
System.out.println(a.i);
^
Q1.java:8: cannot resolve symbol
symbol : variable i
location: class Q1
System.out.println(Q1.i);
^
2 errors

so looks like anything inside static block is ignored. But why, I cant say.. may be someone else can shed some light!
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think scope of the variable come into the picture here. The vatiable i defined within the static block is not visible outside the block.

Can anyone please correct me or explain it in the better way, if any?
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe it is the order in which the initializations are being performed. In this case, the static initializers of the class (i.e. i=10) execute prior to the instance initializer (i.e. i=20).
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Baps Vakkalagadda mentioned, the variable printed in main method is the instance variable i. Variable i declared inside the static initializer is not available outside of the static block.

Regards
-SV
 
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