• 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

static block

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code will give

1: public class Q8
2: {
3: int i = 20;
4: static
5: {
6: int i = 10;
7:
8: }
9: public static void main(String[] args)
10: {
11: Q8 a = new Q8();
12: System.out.println(a.i);
13: }
14: }


in my opinion it should be 10 as static block is loaded first bt it was 20 ....Please explain
 
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 anshi,

have a look at the highlighted part:

Edit: Hint, there are two problems which prevent your expected output.
[ July 14, 2007: Message edited by: Manfred Klug ]
 
anshi kohli
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int is unknown at the time of loading???
 
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

Originally posted by anshi kohli:
int is unknown at the time of loading?

i is a local variable in the sttic initializer block.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Q8
{
int i = 20;
static
{
int i = 10;
}
public static void main(String[] args)
{
Q8 a = new Q8();
System.out.println(a.i);
}
}

Here this code compiles fine

1.int i 20 (instance block)
2.when the static block is run here the int i=10; is intiliazed but lost
after the exceution of the STATIC BLOCK.
3.o/p is 20
ABout static blocks K&B pg.224-226


public class Q8
{
{
int i = 20;
}
static
{
int i = 10;

}

public static void main(String[] args)
{
Q8 a = new Q8();
System.out.println(a.i);
}
}

But compile this it wont(for your understanding)
because the i is within instance block which will not be accessible as
Instance block and static block are excution oriented.
 
saravana.T kumar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by saravana.T kumar:
public class Q8
{
int i = 20;
static
{
int i = 10;
}
public static void main(String[] args)
{
Q8 a = new Q8();
System.out.println(a.i);
}
}

Here this code compiles fine

1.int i 20 (instance )
2.when the static block is run here the int i=10; is intiliazed but lost
after the exceution of the STATIC BLOCK.
3.o/p is 20
ABout static blocks K&B pg.224-226


public class Q8
{
{
int i = 20;
}
static
{
int i = 10;

}

public static void main(String[] args)
{
Q8 a = new Q8();
System.out.println(a.i);
}
}

But compile this it wont(for your understanding)
because the i is within instance block which will not be accessible as
Instance block and static block are excution oriented.

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static blocks cannot access instance variables. Stop and think about it for a second. If their are 7 instances of the class, which instance would it access, umm, arrr, yeah, as such remember it like this

static{}
can only call static methods and access static variables

{} //Initialisation block
can access static methods, static variables, instance variable (this) and instance methods (this)
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anshi,


1: public class Q8
2: {
3: int i = 20;
4: static
5: {
6: int i = 10;
7:
8: }
9: public static void main(String[] args)
10: {
11: Q8 a = new Q8(); //Line 1
12: System.out.println(a.i);
13: }
14: }
[/QB]


you are right that static block will be executed first,
but called before creating object.

here there is a need to remember a single point
1.static variable cannot be referenced with object. so a.i cannot be a static variable

Thanks
[ July 15, 2007: Message edited by: madhu v pe ]
 
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

Originally posted by madhu v pe:
1.static variable cannot be referenced with object. so a.i cannot be a static variable

That is not correct.In this case, the important point to remember is, that the compiler only uses the type of the reference variable, and not the value.
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When a variable is declared in the initializer block it is only within the scope of that block.You check with giving any type of variables.Scope is the thing which matters.Put a out.print in that block..Got it!!
 
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ganesh Kumar:
When a variable is declared in the initializer block it is only within the scope of that block.You check with giving any type of variables.Scope is the thing which matters.Put a out.print in that block..Got it!!



True and that is the reason following code will not compile.

 
Doody calls. I would really rather that it didn't. Comfort me wise and sterile tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic