• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Memory allcocation at time of loading class..

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI, friends..

i am not getting how actual memory allocation is take place at time of loading a class..

how staic block is executed....

as we have thought that in java we can`t use any variable before initialization then here why System.out.println(staticBlock.i); is printing value 0...


public class staticBlock {


int j =9;
static{
System.out.println(staticBlock.i);
}

static int i =10;

public static void main(String args[])
{
System.out.println(staticBlock.i);
}

}
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by kaushik vira:
as we have thought that in java we can`t use any variable before initialization then here why System.out.println(staticBlock.i); is printing value 0...

Since the compiler doesn't try to find all occurences where you use a variable before it's initialized. Another example that compiles:
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well! Declaring static variable i after the static initialization block throws me a Compiler error: "Cannot reference a field before it is defined"

However if i declared "i" before static initialization block , It compiles and gives a result 10. [The value assigned to the variable]

Is this expected and correct?
Does the order of declaration matters when it comes to declaring members of class, I thought java uses forward refrencing.
I have 1.5 compiler.
Thanks
Deepak
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deepak,

have a look at Forward Referencing
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kaushik,

static variables will get initialized to 0 by default.

Thanks
 
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic