• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

reference*might* not be initialized

 
Ranch Hand
Posts: 45
IntelliJ IDE Python Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am curious about wording when having warning about uninitialized variables.
Java tells that variable might not be initiialized instead it is not initialized.
Is there a case in which variable is initialized and Java still throws this runtime error or is it just a word and I should just move on with my life?
 
Marshal
Posts: 5112
319
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Giving an example of when you saw this warning might give a bit of context for your question. Otherwise the best I can offer is "it depends".
 
Marshal
Posts: 74341
334
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two scenarios where I have seen that error message.
  • 1: Line 7 doesn't initialise s. You only see the problem if you declare the field as final. Non‑final fields default to 0/false/null, but final fields don't have a default initial value.
  • 2: A local variable (line 12) doesn't have a default initial value, so you must initialise it explicitly before use. You can get similar errors if you have multiple execution paths and one of them fails to initialise a local variable.
  •  
    Saloon Keeper
    Posts: 24496
    167
    Android Eclipse IDE Tomcat Server Redhat Java Linux
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I think you can also get that message if the initialization is conditional.
     
    Andrzej Zahorski
    Ranch Hand
    Posts: 45
    IntelliJ IDE Python Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Tim Cooke wrote:Giving an example of when you saw this warning might give a bit of context for your question. Otherwise the best I can offer is "it depends".



    I just thought about it when explaining references with simple example:


    As far as I know  and with taking into account good examples from previous answer, there is no possibility of having iniitialized variable that might be not initialized and it is just the wording that is not deterministic enough.
     
    Saloon Keeper
    Posts: 1603
    51
    Eclipse IDE Postgres Database C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    They use the same wording for the following two cases, which is confusing you:





    There are very specific rules about what errors the compiler must emit and must not emit in order to make every piece of Java code either compile or fail to compile everywhere.

    This results in a lot of questions about "Why does/doesn't this fail to compile?" where they give an example that we can see is obviously right or wrong by tracing thru the execution of code that we can see right there.

    There are very strict rules specified in the JLS about what the compiler is or is not allowed to assume or deduce from looking at the code.

    There are many, many questions on this forum about which things the compiler will or will not flag as compile errors and why.

    This one is more subtle, because they use the same wording for "definitely not initialized no matter what happens at runtime" and "depending on runtime behaviors the compiler isn't trying to predict or evaluate will not be initialized".

    I wouldn't worry too much about this one, but there are a lot of questions, at least from those taking a certification exam, about what will be a compile error versus a possible or definite runtime error.

    The great thing about Java compared to other languages some of us also work in is that at least there are very definite rules for what will and what will not compile.  They are all in the Java Language Specification.  Few people seek to memorize all of them, but it is great that these things are defined.

    For many years of my life I supported a very large code base in C/C++, that ran on about 20 different compilers.

    How often did something compile on say, 5 of them and fail on 15, or on 16 and fail on 4?
    Only literally every day!!
    That was "fun".  You will have to fun in other ways when working in Java.
     
    Tim Cooke
    Marshal
    Posts: 5112
    319
    IntelliJ IDE Python Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I get a compile error for that code.



    Admittedly my javac is quite old but it's curious that you report only seeing a warning

     
    Jesse Silverman
    Saloon Keeper
    Posts: 1603
    51
    Eclipse IDE Postgres Database C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Tim:

    I am thinking the OP may have used the word "warning" somewhat carelessly.

    I definitely have seen this as an actual warning in other languages I need to work in, including when it was like "warning -- this very bad thing you should never do just happened".  The code compiled (depending on settings) and you were playing the slot machines to find out what would happen at runtime (also often dependent upon compile settings).

    But as far as I recall, this message is always associated with a compile failure in Java since way back.

    There are warning class messages, usually about type-unsafe operations or deprecated usages, I don't think this was ever one of those.
     
    Andrzej Zahorski
    Ranch Hand
    Posts: 45
    IntelliJ IDE Python Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you for posting this reference it will be nice addition to official tutorial and books.
    This issue is not a regular path of my learning process, I derailed off it, because I simply thought that word "might" sounds quite mysterious and it stands out ;).

    My learning method is to create general topic like "what is an object" and fill that void with knowledge from various different sources and if I bump into something new, that demands explanation, I create empty page for it that will be taken into consideration when I will be in the nearest possible topic to this new occurrence.

    Off-topic:  in terms of fun, I started sharing my (small) experience (I am strong proponent of the idea that knowledge and technology should be open to everyone) and I created github webpage: https://codenotes.dev.
    It is early work in progress, but if you wish to share your programming interests, I can add you to the repo
    Everything is written in Markdown with addition of Mermaid and MathJax (and some other goodies like links in Roam-Research style, admonitions, but it is optional). Page itself is generated by Python mkdocs which soon be automated to github workflow so aspect of maintenance will be simplified to just editing the documents.
     
    Jesse Silverman
    Saloon Keeper
    Posts: 1603
    51
    Eclipse IDE Postgres Database C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    More than you probably want to know, but some stuff discussed somewhat recently about what gets flagged as an error and why:

    https://coderanch.com/t/744012/certification/rule-thumb-compiler-catch

    The basic fact that your Java code will either compile or not compile, rather than "it depends on which platform you are running it on and what compile settings you have chosen" is really awesome, but you will never fully appreciate it if you only use Java.  It is kind of a big deal tho.
     
    Tim Cooke
    Marshal
    Posts: 5112
    319
    IntelliJ IDE Python Java Linux
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    A reminder of the importance of being precise in your language when talking about programming.

    A fundamental quality problem with programming is that programming languages, computers, and other digital machines are such precise system and require absolute precision in thought and syntax to control them, yet that work is being done be the most imprecise of creatures, us humans.

    We can help ourselves greatly by being as precise in language as we can be when working with and discussing programming.
     
    Tim Cooke
    Marshal
    Posts: 5112
    319
    IntelliJ IDE Python Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Andrzej Zahorski wrote:I created github webpage: https://codenotes.dev.


    Kudos on the cat gif on your homepage. It's a long agreed standard for any good web resource.
     
    Andrzej Zahorski
    Ranch Hand
    Posts: 45
    IntelliJ IDE Python Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks, I wanted to give it a bit of a character

    You can send me your github account with Purple Mooseage ;)

    It doesn't really matter that much which topic would you choose as long it has coherent narration, it tackles programming and it might be useful for other people.
    Personally I have planned:
  • Core Java
  • More advanced stuff like networking features of Java
  • Spring
  • Maven
  • Basics of algorithms
  • Core Python
  • Django (not sure tho)
  • HTML + CSS + JS
  • Intro to Linux and DevOps
  •  
    Andrzej Zahorski
    Ranch Hand
    Posts: 45
    IntelliJ IDE Python Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Jesse Silverman wrote:Hi Tim:

    I am thinking the OP may have used the word "warning" somewhat carelessly.

    I definitely have seen this as an actual warning in other languages I need to work in, including when it was like "warning -- this very bad thing you should never do just happened".  The code compiled (depending on settings) and you were playing the slot machines to find out what would happen at runtime (also often dependent upon compile settings).

    But as far as I recall, this message is always associated with a compile failure in Java since way back.

    There are warning class messages, usually about type-unsafe operations or deprecated usages, I don't think this was ever one of those.



    Sorry it is my mistake and yes, it fails to compile, so it is definitely not a warning. I was so focused on "might" so I just wrote everything else without much consideration which generated confusion.
     
    Tim Cooke
    Marshal
    Posts: 5112
    319
    IntelliJ IDE Python Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Andrzej Zahorski wrote:You can send me your github account with Purple Mooseage ;)



    You can find it indirectly through the link in my profile. Be warned though, my github is a barren wasteland for 11 months of the year then kicks off in December for Advent of Code, then back to tumbleweed in January... or mid December when it gets too difficult to carry on.
     
    Campbell Ritchie
    Marshal
    Posts: 74341
    334
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Tim Cooke wrote:. . . curious that you report only seeing a warning . . .

    Yees, it is a compile time error.

    Jesse Silverman wrote:. . . very strict rules specified in the JLS about what the compiler is or is not allowed . . .

    The javac tool doesn't execute any code, nor does it reason what the result of System#nanoTime() is. It leaves all that sort of thing to runtime.
    Maybe this JLS (=Java® Language Specification) section gives you the details, or a link to another specification. Local variables and final fields must be definitely assigned to before they can be used. There are more rules about final variables being assigned to.
     
    Campbell Ritchie
    Marshal
    Posts: 74341
    334
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Yesterday, I wrote:. . . Maybe this JLS (=Java® Language Specification) section gives you the details . . .

    It would appear it does; that section starts,

    Every local variable declared by a statement (§14.4.2, §14.14.1, §14.14.2, §14.20.3) and every blank final field (§4.12.4, §8.3.1.2) must have a definitely assigned value when any access of its value occurs. . . . or a compile-time error occurs.

     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic