Java: how to initialize private final int value with if-else in constructor?
Posted
by HH
on Stack Overflow
See other posts from Stack Overflow
or by HH
Published on 2010-04-12T02:57:54Z
Indexed on
2010/04/12
3:33 UTC
Read the original article
Hit count: 370
$ javac InitInt.java
InitInt.java:7: variable right might not have been initialized
InitInt(){}
^
1 error
$ cat InitInt.java
import java.util.*;
import java.io.*;
public class InitInt {
private final int right;
InitInt(){}
public static void main(String[] args) {
// I don't want to assign any value.
// just initialize it, how?
InitInt test = new InitInt();
System.out.println(test.getRight());
// later assiging a value
}
public int getRight(){return right;}
}
Initialization problem with Constructor, due to if-else -loop
InitInt{
// Still the error, "may not be initialized"
// How to initialise it, without removing if-else?
if(snippetBuilder.length()>(charwisePos+25)){
right=charwisePos+25;
}else{
right=snippetBuilder.length()-1;
}
}
© Stack Overflow or respective owner