Nilesh Patel
SCJP 1.5 - 87%
Nilesh Patel
SCJP 1.5 - 87%
SCJP 5
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.
SCJP 5.0 - 95%<br />Preparing for SCWCD
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???
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???
SCJP 5.0 - 95%<br />Preparing for SCWCD
Originally posted by AMAZER HA HA:
Is there any difference in between the two..??
SCJP 5.0 - 95%<br />Preparing for SCWCD
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.
SCJP 5.0 - 95%<br />Preparing for SCWCD
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.
SCJP 5.0 - 95%<br />Preparing for SCWCD
why the compiler doesnot gives an error for this.
SCJP 5.0 - 95%<br />Preparing for SCWCD
Originally posted by AMAZER HA HA:
[QB]Now after putting the SOP in the static block i am getting the compile time error..
SCJP 5.0 - 95%<br />Preparing for SCWCD
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);
}
}
SCJP 5.0 - 95%<br />Preparing for SCWCD
One more thing i got..
I called the variable x using Class
Now no errors... I am not clear what is happening .???
When evil is afoot and you don't have any arms you gotta be hip and do the legwork, but always kick some ... tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|