This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Cloud Application Architecture Patterns: Designing, Building, and Modernizing for the Cloud and have Kyle Brown, Bobby Woolf and Joseph Yodor on-line!
See this thread for details.
  • 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:

compile time constants

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
package
sample;
public
class StaticVar {
/**
* @param args
*/
public static void main(String[ ] args) {
// TODO Auto-generated method stub
System.
out.println(x);
}
private static int x=getValue();
private static int y=5;
public static int getValue(){
return y;
}
}

how the output of program is 0 instead of 5 i think it may be of runtime constants can anyone explain the why the output is like that and what is runtime constants
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

private static int x=getValue();


During this assignment, the value of static instance variable Y is 0 and is not initialized to 5. In the next line only you are initializing Y to 5. That's why the output is 0.

If the output should have been 5, then you have to initialize the Y value before you actually use that value. Refer the below code to get the Output of 5 :

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic