Results 1 to 5 of 5
  1. #1
    Black_Eye is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default tricky initial value problem

    public class Example
    {
    private int x; // LINE ALPHA

    public void method1( int a)
    {
    int x; // LINE BETA

    ...
    ...
    ...
    }
    }

    What is the intial value of x on LINE ALPHA

    What is the initial value of x on LINE BETA

    my guess is that they're both ZERO
    but I feel like my teacher is tricking me!!!
    Last edited by Black_Eye; 10-19-2010 at 08:33 AM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Black_Eye View Post
    public class Example
    {
    private int x; // LINE ALPHA

    public void method1( int a)
    {
    int x; // LINE BETA

    ...
    ...
    ...
    }
    }

    What is the intial value of x on LINE ALPHA

    What is the initial value of x on LINE BETA

    my guess is that they're both ZERO
    but I feel like my teacher is tricking me!!!
    Nope, the first x is initialized to 0 (zero) unless you initialize it to something else; the second x is a local variable and local variables aren't automatically initialized; you have to do it yourself (or assign a value to it before you use that value of that x).

    kind regards,

    Jos

  3. #3
    Black_Eye is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    ?? there should be an answer to LINE BETA

  4. #4
    Black_Eye is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    is the answer: local variable not initialized?

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Black_Eye View Post
    is the answer: local variable not initialized?
    Yup, that's what I said in my previous reply.

    kind regards,

    Jos

Similar Threads

  1. Initial capacity of ArrayList
    By bugger in forum New To Java
    Replies: 12
    Last Post: 12-30-2009, 08:16 AM
  2. How to set the designer initial properties when making web report
    By freezea in forum Reviews / Advertising
    Replies: 0
    Last Post: 06-04-2009, 04:25 PM
  3. Tricky but very interesting problem
    By ravjot28 in forum New To Java
    Replies: 4
    Last Post: 06-26-2008, 01:43 PM
  4. tricky indexOf implementation -- Help!!
    By definewebsites in forum New To Java
    Replies: 3
    Last Post: 12-10-2007, 12:48 AM
  5. Replies: 1
    Last Post: 08-13-2007, 03:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •