aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static " Watch "Static " New topic
Author

Static

Rahul Kumar
Greenhorn

Joined: Aug 01, 2006
Posts: 16
hi ,

What wud be the output , kindly provide me the explanation too....?

class A{
static{
int x=8;
}
static int x=5;
public static void main(String a[]){
System.out.println(x);
}
}
Nidhi Jain
Ranch Hand

Joined: Mar 26, 2006
Posts: 31
It will Print 5.
Nidhi Jain
Ranch Hand

Joined: Mar 26, 2006
Posts: 31
B'coz void is the Static method and static method can acess the static method directly.
Rahul Kumar
Greenhorn

Joined: Aug 01, 2006
Posts: 16
hi Nidhi,


Cant get....
Nilesh Patel
Ranch Hand

Joined: Feb 02, 2006
Posts: 91
Here when class load all static instance variable and block initiallizing according to its order...

here if you change ..





no problem in output because you have declare local variable of static bock but if you change




output will be "8" insted of "5" because static intialize according to declaration of static order...


And last





it will give error!!! Okay


Nilesh Patel
SCJP 1.5 - 87%
Praveen Babu
Ranch Hand

Joined: Jul 30, 2006
Posts: 138


this compiles fine for me and prints 5.Why is this so ?
Nilesh Patel
Ranch Hand

Joined: Feb 02, 2006
Posts: 91
it will give the error -

"Illigal Forward reference"
Anthony Karta
Ranch Hand

Joined: Aug 09, 2004
Posts: 342
"static" place holder is like static method but... it Always get calls whenever we instantiate a class. It mostly used for initialisation.

Hope the code below make it clear.



SCJP 5
vinod balaji
Ranch Hand

Joined: May 18, 2006
Posts: 84
class vinod
{
static{x=10;}
static int x=15;

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

This code compiles fine, it is not showing illegalforward reference error.Why it is so.
Sriram.Narayanan Thiagarajan
Greenhorn

Joined: Jul 21, 2006
Posts: 26
hi Nilesh Patel

I tried your last option , not giving any error
Iam using jdk1.5 . output is 5


class ss{


static{ x=8; //Remove "int"
}

static int x=5;


public static void main(String a[]){
System.out.println(x);
}
}
Ivan Rebrov
Ranch Hand

Joined: Jul 09, 2006
Posts: 30
Originally posted by vinod balaji:
class vinod
{
static{x=10;}
static int x=15;

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

This code compiles fine, it is not showing illegalforward reference error.Why it is so.


Sure it compiles!

1. x is a static variable, so it will be accessible as soon as a class is first loaded in memory.

2. static{x=10;} - is a static initialization block of this class so it runs AFTER the veriables of class are declared

this code can be compiled also without error:



SCJP 5.0 - 95%<br />Preparing for SCWCD
Rahul Kumar
Greenhorn

Joined: Aug 01, 2006
Posts: 16
hi guys...

I am again confused... Can anybody tell me what actually happens when we declare the static block.. how the flow goes ???
and if we have both static block and static methos what actually happens which is called first???
mithun gooty
Ranch Hand

Joined: Mar 14, 2006
Posts: 33
see the program excecution starts from the first line and then second and so on... so in this code snippet what happens is that first the static variable gets intialized to 8 in the static initializer block.. then again it is chabged to 5... so the output is 5 in the print statement..
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Originally posted by AMAZER HA HA:
hi guys...

I am again confused... Can anybody tell me what actually happens when we declare the static block.. how the flow goes ???
and if we have both static block and static methos what actually happens which is called first???


static block is called at the time of initialization of class. Static method is called when you actually call it explicitly.



When you run Test class by:

java Test

It means you want to execute Test.main()

Here is the steps of execution

1. First Test class gets loaded in JVM.
2. Then it gets initialized i.e., its static variables and static blocks gets executed in the order of their appearance. But no static methods gets executed at this time. After the initialization of class, JRE will start main method.

Program starts when main start and ends when main end.

Here is the output of above code:

In static block
In main
In staticMeth


By the way, is it your real last name HA HA?


Naseem
[ August 08, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
Ivan Rebrov
Ranch Hand

Joined: Jul 09, 2006
Posts: 30
Originally posted by AMAZER HA HA:
hi guys...

I am again confused... Can anybody tell me what actually happens when we declare the static block.. how the flow goes ???
and if we have both static block and static methos what actually happens which is called first???





try this:
Rahul Kumar
Greenhorn

Joined: Aug 01, 2006
Posts: 16
hi Naseem n all,

Well now i am pretty much clear with this..
but still

public class A{
static int x=1;
static {
int x=9;
}
p.s.v.m()
{
sop(x);

}

is same as

public class A{
static {
int x=9;
}

static int x=1;

p.s.v.m()
{
sop(x);

}

Is there any difference in between the two..??

Well Mr. Naseem FYI my last name is not HA HA... hope
it doesnt matter to you too..???
Ivan Rebrov
Ranch Hand

Joined: Jul 09, 2006
Posts: 30
Originally posted by AMAZER HA HA:

Is there any difference in between the two..??



Thre is no difference at all, because "int x=9;" is a declaration of LOCAL variable in STATIC initialization block. As soon as this block finishes, this variable doesn't exist any more.
Andy Morris
Ranch Hand

Joined: May 30, 2004
Posts: 78
Ivan Rebrov, your code sample previously has nothing wrong with it. You state 'it wont be compiled (Cannot reference a field before it is defined)', this is not accurate.

Declarations are evaluated before initialiser blocks.
[ August 08, 2006: Message edited by: Andy Morris ]
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Initialization

all static variables and static initializer blocks are run first as in the same order as written in the source java program when the first time loading class.
Ivan Rebrov
Ranch Hand

Joined: Jul 09, 2006
Posts: 30
to Andy Morris

Your message is not accurate , i meaned only the following commented line of code:

"//System.out.println("b in init blck=" + b);"

And this line won'be compiled because:

"Cannot reference a field before it is defined"

Try it!
Rahul Kumar
Greenhorn

Joined: Aug 01, 2006
Posts: 16
hi,

I agree with Andy.. i checked the code and run it ..it has not given
any error..

public class A{
static{
x=6;
}
static int x=4;

PSVM(){
SOP(x);
}
}

But the output is 4 not 6.
Rahul Kumar
Greenhorn

Joined: Aug 01, 2006
Posts: 16
well if both static variables and static block is present which is run first...

Well Mr. Owen if it is in the order they come in the java source program why the compiler doesnot gives an error for this.

public class A{

static{
x=5;
}
static int x=9;
}
Ivan Rebrov
Ranch Hand

Joined: Jul 09, 2006
Posts: 30
Originally posted by AMAZER HA HA:
hi,

I agree with Andy.. i checked the code and run it ..it has not given
any error..

public class A{
static{
x=6;
}
static int x=4;

PSVM(){
SOP(x);
}
}

But the output is 4 not 6.



Sorry, you didn't get the idea.


insert this

"System.out.println(x);" in your initialization block.

You See?

Now try to put the same init-block AFTER the declaration on "x"
Ivan Rebrov
Ranch Hand

Joined: Jul 09, 2006
Posts: 30

why the compiler doesnot gives an error for this.


Because DECLARATION of variable takes place earlier then all INITIALISATION blocks run
[ August 08, 2006: Message edited by: Ivan Rebrov ]
Rahul Kumar
Greenhorn

Joined: Aug 01, 2006
Posts: 16
Now after putting the SOP in the static block i am getting the compile time error..

public class staticTest {

static{
x=8;
System.out.println("inside static :"+x);
}
static int x=5;
public static void main(String[] args) {
System.out.println("value of is :" + x);

}

}

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

at staticTest.main(staticTest.java:9)
Ivan Rebrov
Ranch Hand

Joined: Jul 09, 2006
Posts: 30
Originally posted by AMAZER HA HA:
[QB]Now after putting the SOP in the static block i am getting the compile time error..


But Only if init-block was coded before declaration of variable. It'ok, you schould live with it. Actually it's not a good idea to write such code.

If you want to get the same result, you schould code your class like this:

amitesh kumar
Ranch Hand

Joined: Aug 01, 2006
Posts: 50
code:



class Vinod
{
static int a = 1;

static {
System.out.println("set 'a' in init block=" + a);
a = 2;
}

static {
// it wont be compiled (Cannot reference a field before it is defined)
//System.out.println("b in init blck=" + b);

b = 2;

// it wont be compiled (Cannot reference a field before it is defined)
// System.out.println("b in init blck=" + b);

System.out.println("set 'b' in init block");
}

static int b=1;

static {
// it wont be compiled (Cannot reference a field before it is defined)
// System.out.println("c in first init blck=" + c);

c = 2;

// it wont be compiled (Cannot reference a field before it is defined)
// System.out.println("c in first init blck=" + c);

System.out.println("set 'c' in first init block");
}

static int c=1;

static {
System.out.println("view 'c' in second init block=" + c);
c = 3;
System.out.println("set 'c' in second init block=" + c);
}

public static void main(String args[])
{
System.out.println("a=" + a);
System.out.println("b=" + b);
System.out.println("c=" + c);
}
}


Hi Andy, I run this code and it gives ClassCastException?. Please Explain
amitesh kumar
Ranch Hand

Joined: Aug 01, 2006
Posts: 50
code:



class Vinod
{
static int a = 1;

static {
System.out.println("set 'a' in init block=" + a);
a = 2;
}

static {
// it wont be compiled (Cannot reference a field before it is defined)
//System.out.println("b in init blck=" + b);

b = 2;

// it wont be compiled (Cannot reference a field before it is defined)
// System.out.println("b in init blck=" + b);

System.out.println("set 'b' in init block");
}

static int b=1;

static {
// it wont be compiled (Cannot reference a field before it is defined)
// System.out.println("c in first init blck=" + c);

c = 2;

// it wont be compiled (Cannot reference a field before it is defined)
// System.out.println("c in first init blck=" + c);

System.out.println("set 'c' in first init block");
}

static int c=1;

static {
System.out.println("view 'c' in second init block=" + c);
c = 3;
System.out.println("set 'c' in second init block=" + c);
}

public static void main(String args[])
{
System.out.println("a=" + a);
System.out.println("b=" + b);
System.out.println("c=" + c);
}
}



Hi Andy, I run this code and it gives ClassCastException?. Please Explain
Rahul Kumar
Greenhorn

Joined: Aug 01, 2006
Posts: 16
hi Andy,

One more thing i got..
I called the variable x using Class

public class staticTest {

static{
x=8;

System.out.println("value of is :" + staticTest.x);
}
static int x=5;

public static void main(String[] args) {
System.out.println("value of is :" + x);

}

}

Now no errors... I am not clear what is happening .???
Ivan Rebrov
Ranch Hand

Joined: Jul 09, 2006
Posts: 30
Cool! That's java! Don't think about it, just try to remember for the exam!

Here is something even more interesting:




[ August 08, 2006: Message edited by: Ivan Rebrov ]
[ August 08, 2006: Message edited by: Ivan Rebrov ]
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023

One more thing i got..
I called the variable x using Class

Now no errors... I am not clear what is happening .???


8.3.2.3 Restrictions on the use of Fields during Initialization
amitesh kumar
Ranch Hand

Joined: Aug 01, 2006
Posts: 50
Friends,

In all of last 3-4 replies there is same concept. For static variables if we access a variable in static block using className then its OK as the varaible has been initialized when class was loaded. Same is the case with nonStatic block.n this case one is accessing 'x' using this and object is there in the memory. But in both cases one cannot access x without using className (for static variable static block) and this (for non-static variable in non-static block).

I hope this explanation will make things clear
Varsha Patil
Greenhorn

Joined: Jun 16, 2006
Posts: 19
Hi All,

Can someone explain wat's happening here

public class Test {

{
x=8;
System.out.println("value of x :" + Test.x);
}
static int x=5;
public static void main(String[] args) {
System.out.println("value of x in main is :" + new Test().x);
}
}

The output is
value of x :8
value of x in main is :8

Can somebody explain
[ August 10, 2006: Message edited by: Varsha Patil ]
sanjeev mehra
Ranch Hand

Joined: Feb 12, 2001
Posts: 93
Seems concept is clear to all, that's happy ending....
I have a request, Not all but most of the participant didn't use the available formatting option(s).
Please use Code formatting option for writing code, that's helpful to read the code.
sanjeev mehra
Ranch Hand

Joined: Feb 12, 2001
Posts: 93


Ans is :

two kind of entities
1. static (e.g. static int i = 0 ; )
2. non-static (e.g. int i = 0 ; )


execution sequence
1.
static gets executed first; & it has a sequence too; that is;
static variable(s) execution;
static block(s) execution;

2.
then non-static with sequence;
variable(s) execution;
block(s) execution;

3.
public static void main (String [] args)
method.




if still you have doubt, then :roll: ; please share.
Varsha Patil
Greenhorn

Joined: Jun 16, 2006
Posts: 19
Thanks for the explanation!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static
 
Similar Threads
static variables
Reg. local variables
a question about static clause
initialisation and reinitialisation doubt.....
Doubt on order of execution