• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Bear Bibeault
  • paul wheaton
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Mikalai Zaikin
  • Piet Souris
Bartenders:

initialization..

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, this question is from whizlabs.practice exam 2


public class ref
{
final static int x=getIt();
static int y=5; If i insert final here output is 5.//
private static int getIt()
{
return y;
}
public static void main(String args[])
{
System.out.println(x);
}
}

output is 0.

can anyone explain, how come the answer is 5 in case of adding "final"?

and i need an example for forward referencing...
please provide me that too.

Thanks
Preetha
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I have to admit it's excellent question.

Because,
when you declare a static attribute as final, it cannot be changed in it's life, not even in construction thread, where final but non-static attributes can be changed.

If you see, It's .class file code, you will see that all static final will be converted to Constant Value, so it may be assigned at compilation time only. [I don't have solid proof to prove, so i said may be, otherwise i am confident enough]. So there is nothing like forward assignment.

And anyhow, outside method body [& static body], there is nothing like top-down sequence execution. isn't it.

For testing i coded this class, i hope it'll help you all to understand.

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Final static variables are initialized during the compile time. So, when you add 'final' in the y declaration, the value of y becomes 5 during the compile time only.
If it is declared as only static, that variable is known to compiler, but it's only when the JVM comes across the y declaration, it assigns y a value of 5.
Please correct me in case I am wrong.
 
Nachiket Patel
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

anyhow, outside method body [& static body], there is nothing like top-down sequence execution. isn't it.



I think , i am wrong here. This example definitely show that it is executing in sequence.
 
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Preetha:
This link will help you clear your doubt: Forward Referencing


Orignially posted by Nachiket Patel:

where final but non-static attributes can be changed.



@Nachiket: you cannot change a final variable's value whether static or not.

In your code snippet if you say:



And if you say:



you'll get a compiler error because you have to initialize the non-static final variables before the constructor completes.

Moreover, the static final variables have to be assigned a value at the time of declaration because they are initialized at compilation time by a process called *constant-folding*.

[ December 07, 2008: Message edited by: Harvinder Thakur ]
[ December 07, 2008: Message edited by: Harvinder Thakur ]
 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
During Initialization of the class, static members, direct/indirect super classes are initialized. After completion of Initialization, main method is invoked. Final variables are initialized at compile-time.Then when are final static variables initialized???
 
author
Posts: 23939
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Final variables are initialized at compile-time.Then when are final static variables initialized???



Well, obviously, this isn't true... Take a look at this snippet from the example.



The variable is declared as final, and there is no way it can be resolved at compile time.

The key is being a compile time constant (of which being final is one criteria). And technically, it is not being "initialized" at compile time, but more like... Since it is a compile time constant, the variable doesn't really exist at all. And everywhere that it is used is hardcoded with the constant value.

Henry
 
Nachiket Patel
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

And if you say:

code:

final int value; // final variable has not been initialized
final static int staticValue = 6676;

public FinalStaticDifference(int p) {
//value = p;
//staticValue =p; If you add this statement it won't compile.
}



you'll get a compiler error because you have to initialize the non-static final variables before the constructor completes.



What i am saying is, now if you remove your comment on
value = p;
then it will work,

so you are assigning value to final in constructor thread. (Only ones)
Yes, that's true that you can assign only once. you cannot change it.
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It seems that the compiler will always report error if any of the final and static final variables is left uninitialized. Only place to initialize them is constructor.
 
What a stench! Central nervous system shutting down. Save yourself tiny ad!
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic