Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8783637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:52:14+00:00 2026-06-13T20:52:14+00:00

class Foo{ public static void main(String args[]){ final int x=101; int y; if(x>100){ y=-1;

  • 0
class Foo{
    public static void main(String args[]){
        final int x=101;

        int y;
        if(x>100){
            y=-1;
        }
        System.out.println(y);
    }
}

Java compiler understands the condition of the if statement is always true and therefore y will always be initialized. No compile error, as expected.

class Bar{
    public static void main(String args[]){
        final int x;
        x=101;

        int y;      
        if(x>100){
            y=-1;
        }
        System.out.println(y);
    }
}

But when I break the declaration and initialization of x into two lines, the compiler does not seem to get that the condition is always true and y will always be initialized.

final int x;
x=101;
byte b;
b=x;
System.out.println(b);

Same thing happens here and the compiler gives a loss of precision error.

final int x=101;
byte b;
b=x;
System.out.println(b);

Again, the compiler can understand that x is inside the range of b.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:52:16+00:00Added an answer on June 13, 2026 at 8:52 pm

    It has to do with how the compiler determines if a statement will be executed or not. It is defined in the JLS #16:

    Each local variable and every blank final field must have a definitely assigned value when any access of its value occurs.

    In your case, the compiler can’t determine that y has been definitely assigned and gives you an error. This is because it would need to determine that the condition is always true and that is only possible if the condition in the if is a constant expression.

    JLS #15.28 defines constant expressions:

    A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:

    • […]
    • Simple names (§6.5.6.1) that refer to constant variables (§4.12.4).

    The JLS #4.12.4 defines constants variables as:

    A variable of primitive type or type String, that is final and initialized with a compile-time constant expression, is called a constant variable.

    In your case, final int x = 101; is a constant variable but final int x; x = 101; is not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public class Foo { public static void main(String[] args) { float f; System.out.println(f); }
Consider this code: class Foo { public void doIt(String... strs) { System.out.println(this is varargs);
what should the following java code do? public class foo{ public static void main(String[]
class Foo { public: void bar(); }; void Foo::bar() { static int n =
The following code: static void Main(string[] args) { Console.WriteLine(0); string h = Foo.X; Console.WriteLine(2);
Consider the following Java class: public class Foo { public static void doStuff() {
Consider the following program: class A { public static void Foo() { } }
class Foo { public static IEnumerable<int> Range(int start, int end) { return Enumerable.Range(start, end);
I have a class class foo { public: foo(); foo( int ); private: static
I understand that in this code: class Foo { public static void method() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.