• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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
 
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic