Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

What is the rule with initializing static variables here?

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following code which is a question on Enthuware that I'm confused about. Is the static variable i2 never initialized at all or is it okay for an instance initializer to do initialize it?

Answer to below question: The code will compile without error and will print 6 40 0 30 5 when run.


______________________________________________________________________________________________________________________________________________________________________________________

 
Bartender
Posts: 1868
81
Android IntelliJ IDE MySQL Database Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All static variables are initialized by the JVM to their base values. So the first code block is exactly the same as the second code block:





Now things do get interesting when you add in the final modifier with static variables. final values are considered constants but they are not all treated the same. If you don't initialize a static variable when you declare it then the JVM won't allow you to use it for a switch statement as shown below.

Problem code:

Correct code:


And please don't forget that static or not, any final variable can only be assigned a value once. The assignment can happen when you declare the variable or later on in your code.  
From what I gather,  there are a few oddities with this (e.g. method parameters), however, this is effectively true for the OCA 808 exam.
 
Heather Keating
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your explanation!
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this topic you'll find a nice overview of the initialization of class, instance and local variables (even when they are marked final). Definitely worth reading!

Hope it helps!
Kind regards,
Roel

PS. Always use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers. I already added the code tags for you. See how much easier the code is to read?
 
The harder you work, the luckier you get. This tiny ad brings luck - just not good luck or bad luck.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic