• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Static

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will Print 5.
 
Nidhi Jain
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
B'coz void is the Static method and static method can acess the static method directly.
 
Rahul Kumar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Nidhi,


Cant get....
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


this compiles fine for me and prints 5.Why is this so ?
 
Nilesh Patel
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it will give the error -

"Illigal Forward reference"
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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.

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:

 
Rahul Kumar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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???
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Ivan Rebrov
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the explanation!
 
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
reply
    Bookmark Topic Watch Topic
  • New Topic