views:

346

answers:

7
$ 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;

    // Design Problem?
    // I feel the initialization problem is just due to bad style.

 InitInt(){}
    InitInt{
           // Still the error, "may not be initialized"
           // How to initialise it?

            if(snippetBuilder.length()>(charwisePos+25)){
                    right=charwisePos+25;
            }else{
                    right=snippetBuilder.length()-1;
            }
    }

 public static void main(String[] args) {
  InitInt test = new InitInt(); 
  System.out.println(test.getRight());
 }
 public int getRight(){return right;}
}

Partial Solutions and Suggestions

  1. use "this" to access methods in the class, instead of creating empty constructor
  2. change final to non-final
  3. with final field value: initialize all final values in every constructor
  4. remove the empty constructor, keep your code simple and clean
+2  A: 

You can't use new with int. int is a primitive, and new is an object operator. Consider using Integer instead, or just assigning an integer literal to it.

Ignacio Vazquez-Abrams
+1  A: 

There's nothing wrong with your if-else statement, and nothing wrong with initializing a final variable within a branching statement in a constructor. I just ran a simple constructor like yours to initialize private int right and it worked fine. Make sure that you are declaring your constructor correctly, as InitInt() { ... }.

The error you posted is because you have in your code InitInt(){}, an empty constructor that does not initialize right. You need to initialize final fields in this and all constructors.

Justin Ardini
Problem A: some methods have not parameters. Problem B: many methods depends on the IniInt(){}. Problem C: to initialise things in InitInt(){...}. How?
HH
Justin Ardini
A: 

Static modifier on right

Woot4Moo
-1 - this completely changes the meaning of `right`.
Stephen C
Actually it doesn't but thanks for the downvote
Woot4Moo
+3  A: 

You mean define, not initialize. The problem you're having (after that pretty radical edit) is you're defining a constructor that doesn't initialize a final variable, which Java doesn't allow -- all finals need to be initialized by the time the instance is finished constructing. Either initialize it in your constructor, or make it non-final

Michael Mrozek
The code you currently have posted isn't valid, for the reasons I said. Did you try making right non-final? If it's final you need to initialize it in the constructor, it's mandatory
Michael Mrozek
A: 

Your constructor is absolutely Okey!!!! The problem is that you left the "right" variable uninitialized.

You have to initialize the "right" variable:

private final int right = 0;
sza
+1  A: 

Yeah, the problem is that one of your constructors doesn't initialize the final variable. In Java final fields have to be initialized at the declaration time OR in EVERY constructor! The default constructor in your example doesn't do that. Remember as well that implementing empty default constructor makes sens only if you want to use inheritance features. If you don't provide a default constructor, but you will some other one Java won't make a hidden default constructor for you, because the default one is not required. So don't implement things like MyClass() {} with no special purpose - keep your code clean and save!

A: 

If you only try to access the methods in the class, use this, instead of creating empty-constructor for it:

import java.io.*;
import java.util.*;

public class FileDir {
        private ArrayList<Integer> lineNumbers;
        FileDir(Integer nth){
                lineNumbers=new ArrayList<Integer>();
                lineNumbers.add(nth);
                // You don't need an empty constructor
                // to call class methods, use "this"
                this.printHello("Davids");
        }
        public static void main(String[] args) {
                FileDir test = new FileDir(7);
                ArrayList<Integer> inteTest=test.getLineNumbers();
                for (Integer i : inteTest)
                        System.out.println(i);
        }
        public void printHello(String name) { System.out.println("Hello "+ name); }
        public ArrayList<Integer> getLineNumbers() { return lineNumbers; }
}
HH