File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Win a copy of
Java Interview Guide
this week in the
Java in General
Static block and static variable
Joined: Jan 24, 2006
Aug 02, 2006 23:54:00
Can anybody clearly explain me that what is executed first? A Static block, a static method or static variables? What is sequence of execution of these following lines?
public class Static
int x = 5;
static int x,y;
public static void main(
System.out.println(x + y + ++x);
public static void myMethod()
y = x++ + ++x;
What will be the output? If you move the statis block below the static variable decalaration, why the output remains same.....
Please reply as soon as possible.
Joined: Jul 28, 2006
Aug 03, 2006 00:35:00
As for i understood, the var i defined in the static block is no way affecting the output..because...its scope is confined to that block...
So the output will be 3
And if you define static block something like below.
static int x,y;
x = 5;
Output will be 23
Here order matters...if you interchange the stmts, you will get compilation error " illegal forward reference"...
So if static methods are initializing any of the static var then order matters..
That is what my understanding...
I agree. Here's the link:
subject: Static block and static variable
a question about static clause
static block understanding
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2015