• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static block

 
anshi kohli
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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
 
Manfred Klug
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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.
 
saravana.T kumar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Jared Folino
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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)
 
madhu v pe
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ganesh Kumar
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • 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!!
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
  • 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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic