Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Initializing Variables

  1. #1
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Initializing Variables

    Not sure if this has been done before, but I couldn't find one with Google or searching here, so thought I'd recreate it.
    The Problem
    Variable abcd has may not have been initialized

    The Scenario
    String someVariable;
    if(1 == 1) //This could be a while-loop, for-loop, switch statement, etc.
    {
      someVariable = "Butterflies";
    }
    System.out.println(someVariable); //The error will be here


    Why the error occurs
    In the code above, it is intuitively obvious that someVariable will be initialized. However, because Java will not hard-code that, the compiler does not have the same intuition. Suppose you modified the if-statement in the above example to use the condition "1 == 2", making the new program:
    String someVariable;
    if(1 == 2) //This could be a while-loop, for-loop, switch statement, etc.
    {
      someVariable = "Butterflies";
    }
    System.out.println(someVariable); //The error will be here
    What is the compiler suppose to print out?

    The Solution
    Initialize your variable to some arbitrary value, or use else statements. Any statement that will always be called, no matter the conditions of the program. Two possible fixes for the example program above would be:
    String someVariable = null; //Give it a value to start with
    if(1 == 2) //This could be a while-loop, for-loop, switch statement, etc.
    {
      someVariable = "Butterflies";
    }
    System.out.println(someVariable); //No error!
    or
    String someVariable;
    if(1 == 2) //This could be a while-loop, for-loop, switch statement, etc.
    {
      someVariable = "Butterflies"; //If true, set the variable here
    }else //If thats not true, go here.  The variable will always be initialized.
    {
      someVariable = "Ladybugs";
    }
    System.out.println(someVariable); //No error!


    Code for linking here
    [url=http://www.javaprogrammingforums.com/java-code-snippets-tutorials/12025-initializing-variables.html]Initalizing Variables by TJStretch[/url]
    Last edited by Tjstretch; October 29th, 2011 at 12:59 PM.

  2. The Following 3 Users Say Thank You to Tjstretch For This Useful Post:

    copeg (October 29th, 2011), Hifzur Rahman (July 21st, 2012), JavaPF (October 30th, 2011)


Similar Threads

  1. Help with clearing variables
    By fmr in forum Other Programming Languages
    Replies: 2
    Last Post: July 18th, 2011, 10:18 AM
  2. Nested-If Problem/ Initializing
    By ak120691 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 2nd, 2011, 08:37 PM
  3. initializing a java JApplet
    By j_a_lyons in forum Java Theory & Questions
    Replies: 0
    Last Post: January 8th, 2011, 04:49 PM
  4. Non-Static variables
    By liloka in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 31st, 2010, 09:13 AM
  5. variables and efficiency
    By catkinq in forum Java Theory & Questions
    Replies: 3
    Last Post: February 7th, 2010, 06:09 AM