class C{
public static void main(String a[]) {
int i1=9;
int i2;
if(i1>3) {
i2=8;
}
System.out.println(i2);
}}
aditya027 0 Newbie Poster
Recommended Answers
Jump to PostYou are trying to print out a variable(i2) which is possibly uninitialized (depends on rutime, i2 won't be initialized if i1 < 3). Either provide an initial value for i2 or write an else statement which takes care of the same.
Jump to Postthe error is a compile time error.The error is:
VARIABLE MIGHT NOT HAVE BEEN INITIALISEDBut after declaring the variable 'i as final' the code runs just fine. Y??
Because when you declare a variable as `final`, the compiler ensures that re-assignment to that variables never happens. Using static program …
Jump to PostIf i1 is not declared `final`, the compiler has no way of knowing in advance that the value of i1 won't change; hence it complains if you don't provide an ELSE clause. When i1 is declared as `final`, the compiler knows for sure that i1 will *always* be 9 and …
Jump to PostNo, I meant i1 since it is the variable which is initialized. Removing or adding the `final` quantifier to i1 changes the way the compiler treats the presented source code. If i1 is made final, the uninitialized state of i2 is acceptable since it is anyways going to get initialized …
Jump to PostYes, that's correct.
All 19 Replies
javaAddict 900 Nearly a Senior Poster Team Colleague Featured Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster
akshay357 0 Newbie Poster
aditya027 0 Newbie Poster
aditya027 0 Newbie Poster
NormR1 563 Posting Sage Team Colleague
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster
aditya shikhar 0 Newbie Poster
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster
NormR1 563 Posting Sage Team Colleague
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster
NormR1 563 Posting Sage Team Colleague
~s.o.s~ 2,560 Failure as a human Team Colleague Featured Poster
aditya shikhar 0 Newbie Poster
jon.kiparsky 326 Posting Virtuoso
WargRider 10 Junior Poster
aditya027 0 Newbie Poster
WargRider 10 Junior Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.