• 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
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

accessing static values

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

This was taken from JDK Specification
http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html#44365

class Z {
static { i = j + 2; }
static int i, j;
static { j = 4; }
}

result in compile-time errors. Variable j was accessed before it is declared.


class Z {
static int peek() { return j; }
static int i = peek();
static int j = 1;
}
class Test {
public static void main(String[] args) {
System.out.println(Z.i);
}
}

produces the output:

0


Question is: how it is printing "0", when the static property needs to be declared before accessing?
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is difference between static blocks and static methods.
Static blocks and variables invokes (initializes) after class load in order they were declared. So you cannot access variable before you declare it. It is because it's done only once and you cant do this explicitly.

Static methods can be invoked from anywere and they can access any static variable.
I dont know how exactly it works from inside.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instance variables will always be initialized to 0 ( default value )if you don't initialize them.
In the above case, you are calling peek() before 'j' is set to 1, that means, j = 0 when you call:

static int i =peek();

Do this and you'll get 1:

static int j = 1;
static int i = peek();

It works.

Sanjeev
 
Badri Sarma
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sanjeev Kulkarni:
In the above case, you are calling peek() before 'j' is set to 1, that means, j = 0 when you call:



Sanjeev, if you look at the code the variable itself is declared after the method. So it should throw exception. So if method able to recognise j then it should be 1 not 0.
 
Badri Sarma
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any inputs on the query?
And also let me know if there are any good tutorials on static variables & method declaration
 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Static blocks and variables invokes (initializes) after class load in order they were declared. So you cannot access variable before you declare it. It is because it's done only once and you cant do this explicitly.



You said the order is important what about this which compiles fine unfortunately:

class Z {
static { j = 1; } // But doesn't j needs to be delclared before
static { i = 2; } // Initializing like i=j results in error unlike initializing like above line . Why is compiler selective?
static int i, j;
}

So because it compiles it's creating a lot of confusion to me. Also, the compiler is selective, if you don't declare the variable j before use, it's OK as long as you initialize j like this j=1 and not j=i !
[ June 10, 2006: Message edited by: Firas Zureikat ]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

class Z {
static { j = 1; } // But doesn't j needs to be delclared before
static { i = 2; } // Initializing like i=j results in error unlike initializing like above line . Why is compiler selective?
static int i, j;
}



1) So, net net, the compiler allows the setting of a static variable before declaring it. It checks whether the static variable is declared somewhere. If yes, it allows the setting of value.

2) Secondly, you can not get the value a variable unless it is declared first. That is why i = j; would be compile-time error. Here you are trying to acces the value of j before declaring it. Not allowed!

3) I know, Badri will argue that, in
class Z {
static int peek() { return j; }
static int i = peek();
static int j = 1;
}

Ironically, when I try

class Z {
static int peek() { return i; }
static int i = peek();
static int j = 1;
}

It still compiles fine. So, looks like, in case of static method calls, the compiler just sees whether the static variable is declared somewhere. If it is declared before, it will return the present value. If it is declared afterwards, the default value is retured.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forward Referencing
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the link about forward referening. It answered all my questions and more!

- Anu
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Z {
static int peek() { return j; }
static int i = peek();
static int j = 1;
}
class Test {
public static void main(String[] args) {
System.out.println(Z.i);
}
}

produces the output:

0

This is very intriguing how it works. When i get its value assigned through the function, j is known(no compilation error) and its value is 0. The forwardRef does not explain this situation. Any more inputs?
 
Badri Sarma
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


I think that java creators are not clear about static variables, some time they talk about
1.Declare variable before use (OR)
2.You can assign variable before declaring (OR)
3.Method will return default value (like in the above case).

For only one situation there are so many exception, Is it not very confusing to learn Java itself.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic