× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Can't initialize static final field in constructor

James Jensen
Feb 25, 2015
<p><code>aspectRatio</code> is <code>static</code>, but you're trying to initialize it in the constructor, where it will be set every time a new <em>instance</em> is created. By definition, that's not final. Try using a static initialization block, instead.</p> <pre><code>public static final float aspectRatio; static { screenWidth = Gdx.graphics.getWidth(); screenHeight = Gdx.graphics.getHeight(); aspectRatio = screenWidth/screenHeight; } public TestBaseClass() { // Any instance-based values can be initialized here. } </code></pre> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/28633458/Can't%20initialize%20static%20final%20field%20in%20constructor/28633526">Stack Overflow</a>.</p>
comments powered by Disqus