wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static block" Watch "static block" New topic
Author

static block

anshi kohli
Ranch Hand

Joined: Jun 30, 2007
Posts: 35
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

Joined: Jun 04, 2007
Posts: 377
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

Joined: Jun 30, 2007
Posts: 35
int is unknown at the time of loading???
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
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

Joined: Jul 13, 2007
Posts: 23
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

Joined: Jul 13, 2007
Posts: 23
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

Joined: Apr 27, 2007
Posts: 25
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

Joined: Apr 21, 2007
Posts: 100
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

Joined: Jun 04, 2007
Posts: 377
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

Joined: Jul 02, 2007
Posts: 113
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

Joined: Sep 16, 2004
Posts: 3636
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.



My blood is tested +ve for Java.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static block