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 2 of 2

Thread: Instance data member vs Local variables (primitive/object reference)

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Instance data member vs Local variables (primitive/object reference)

    just a simple question: why does a local variable should be initialized before a program's running stage? considering that this variable will be used somewhere local, while a data member can be used even if it is not initialized in a constructor or an instance method.
    does the compiler always assign an offset values in data members when they are not initialized explicitly?


  2. #2
    Member
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    2
    Thanked 7 Times in 6 Posts

    Default Re: Instance data member vs Local variables (primitive/object reference)

    Here is an explanation for both of your questions: oop - Uninitialized variables and members in Java - Stack Overflow
    The compiler forces you to initialize local variables partly because it can not figure out the full logic of your program flow.

    Edit: My compiler optimizes away the first increment but not the second. Whether this should be an error or a warning is
    debatable but that is the way it is in java.
    public void foo() {
         int x;
         final int y = 0;
         if(false)
    	x++;
         if(y == 0)
    	x++;
    }
    Last edited by dabdi; September 23rd, 2011 at 02:12 AM.

  3. The Following User Says Thank You to dabdi For This Useful Post:

    chronoz13 (September 23rd, 2011)

Similar Threads

  1. Replies: 10
    Last Post: April 3rd, 2013, 04:16 PM
  2. Tutorial: Primitive Data Types
    By newbie in forum Java Programming Tutorials
    Replies: 3
    Last Post: July 6th, 2012, 12:56 AM
  3. Primitive data type
    By stuart harper in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 17th, 2011, 01:14 PM
  4. [SOLVED] cannot referenced <instance data member> before super type constructr has been called
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: August 8th, 2011, 12:26 PM
  5. Fitting a large primitive into a small reference variable
    By Phobia in forum Java Theory & Questions
    Replies: 15
    Last Post: October 23rd, 2009, 04:10 PM