0
final int a, b;

if (condition1) {
    a = get(dynamicValues);
    b = get(dynamicValues);
}

if (condition2) {
    int c = b + a;
    display(c);
} 

In this type of code the compiler is asking to intialize a and b, which I can't do until the condition1 is met. and a and b being final is mandatory as the values of hours and minutes keep changing. The above written code is very generalized just to give you an idea of my problem.

UPDATE: The problem here is condition2 is one of the else conditions of condition1. So can't use else statements too. Dynamic values here are the values like "hrs and mins" which always change. when condition1 is met a and b are intialised with hrs and mins, condition2 will definitely happen after condition1 at some point of time. So the time difference between condition2 and condition1 needs to be calculated.

  • 1
    Why you need `a` and `b` final? – lifeisfoo Aug 25 '15 at 19:43
  • *"The problem here is condition2 is one of the else conditions of condition1"* And how should that work? How should `a` and `b` initialized _and_ `else if(condition2)` (your code is a bit different here) be entered? – Tom Aug 25 '15 at 20:35
  • as I already said.. a and b are intialised with hrs and mins in my android code and the change in hrs and mins is also causing change in my a and b values. so I want them to final and note the time when condition1 is met. – Teja Karlapudi Aug 25 '15 at 20:57
  • when condition1 is met a and b are intialised, condition2 will definately happen after condition1 at some point of time. So the time difference between condition2 and condition1 needs to be calculated. – Teja Karlapudi Aug 25 '15 at 21:00

3 Answers3

3

if condition1 is false a and b remain not initialized. That's why the compiler is complaining. Add the else branch and it will compile correctly

if(condition1)
 {
  a=cal.get(Calendar.HOUR);
  b=cal.get(Calendar.MINUTE);
 } else {
   a = 0;
   b = 0;
 }
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
1

Forgive me if I have not understood your problem correctly :). It seems to me that perhaps it's easier to wrap up your 'final' variables into an immutable class. Then create an instance of such a class in 'condition1'. Here's what I mean...

import java.util.Scanner;
import java.util.Calendar;

public class ImmutableHourMinute {

    private final int a;
    private final int b;

    public ImmutableHourMinute(final int $a, final int $b){
        this.a = $a;
        this.b = $b;
    }

    public int getA() {
        return a ;
    }

    public int getB() {
        return b;
    }

    public static void main(String... args){
        // Requirements:
        // 1. Hour and minute are set at condition == 1;
        // 2. Hour and minutes must never be changed.
        System.out.println("Please type 'start':");
        Scanner scanner = new Scanner(System.in);
        ImmutableHourMinute hourAndMinute = null; // Can be initialized properly later

        while(!scanner.nextLine().equals("quit")){
            System.out.println("Please enter condition (1 or 2):");
            int condition = scanner.nextInt();

            if(condition == 1){
                Calendar rightNow = Calendar.getInstance();
                int a = rightNow.get(Calendar.HOUR);
                int b = rightNow.get(Calendar.MINUTE);
                System.out.println(String.format("(Creating ImmutableHourMinute(a=%1$d, b=%2$d)", a, b));
                hourAndMinute = new ImmutableHourMinute(a, b);
            }else if (condition == 2){
                display(hourAndMinute.getA() + hourAndMinute.getB());
            }

            System.out.println("Please enter condition (1 or 2):");
        }

    }

    private static void display(final int displayValue){
        System.out.println(String.format("Displaying the time set in condition 1 :%1$d", displayValue));
    }
}

And here is a sample of the output:

$ java ImmutableHourMinute
Please type 'start':
start
Please enter condition (1 or 2):
1
(Creating ImmutableHourMinute(a=9, b=56)
Please enter condition (1 or 2):
Please enter condition (1 or 2):
2
Displaying the time set in condition 1 :65

note : such and immutable class already exists(if you're using Java SE 8)... java.time.LocalDateTime

welterw8
  • 106
  • 1
  • 7
  • Hi, your answer works, but could you explain why you use $a? I remove $ it works either. I don't think java uses $ like C++. – Kun Aug 26 '15 at 18:41
  • Hi, I just used the $a so that it is utterly clear it's differernt from 'this.a'. just a personal preference. Sometimes scope/visibility rules of variables can cause confusion, so I try to avoid as much as possible :) – welterw8 Aug 26 '15 at 20:38
0
public class time{
private int a=0;
private int b=0;

public int get()
  {
    return a+b;
  }

public void set()
  {
    this.a = cal.get(Calendar.HOUR);
    this.b = cal.get(Calendar.MINUTE);
  }
}

In your main class, call the time class method(out of main method, at very begining),

public final Time t = new Time();

In main method:

if(condition1)
{
 t.set();
}
if(condition2)
 {
  display(t.get());
} 
Kun
  • 580
  • 2
  • 13
  • That didn't work because if a and b are initialized to zero and they are final, we cant change their values later (in conditions) – Teja Karlapudi Aug 25 '15 at 19:54
  • oooooh,sry misunderstood, I got your point. Let me explain, in JVM final variables should be initiated before any object creation. I modified the answer and try it. – Kun Aug 25 '15 at 20:04
  • Didn't work...by the way you forgot "int" after static and after correcting that... ERROR: Illegal modifier for the variable a; only final is permitted – Teja Karlapudi Aug 25 '15 at 20:12
  • check it this time, it was mb, static should be claimed at very begining. – Kun Aug 25 '15 at 20:16
  • I want to use condition1's hour and minute values in a and b. your else if block will get condition2 values. and in my case condition1 will definitely occur before condition2 so.. else if doesn't help. – Teja Karlapudi Aug 25 '15 at 20:25
  • In your case, what is the relationship between condition1 and 2? If no relation, just remove "else". I think you can use a method to finish this function.I'll modify the answer. – Kun Aug 25 '15 at 20:36
  • a and b values will come from condition1 only and the calculation of c is done in condition2. these things are mandatory – Teja Karlapudi Aug 25 '15 at 20:42