Results 1 to 3 of 3
  1. #1
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default initialization value problem

    Hi,

    I am confused about the initialization values in Java. I tried the following code and got an error:


    Java Code:
    boolean flag;
    System.out.println("Printing boolean flag's value: "  + flag);
    Exception:
    Java Code:
    The local variable flag may not have been initialized
    I assumed that flag will have false in it. I tried a boolean array and it had false in all the indexes by default.

    Please guide me.

    Thanks a lot.

  2. #2
    kureikougaiji is offline Member
    Join Date
    Nov 2007
    Posts
    15
    Rep Power
    0

    Default hmm..

    it depends on what you want.. if you want it to be false in the start of your program, initialize it..

    boolean flag=false;

  3. #3
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    This behavior doesnot work with local variables .. If you have code like this then it will be perfectly okay
    Java Code:
    public class NativeTypeInitializationDemo {
    	public boolean flag;
    	
    	NativeTypeInitializationDemo(){
    		System.out.println("apple" + flag);
    	}
    	
    	public static void main(String[] args){
    		new NativeTypeInitializationDemo();
    	}
    }
    dont worry newbie, we got you covered.

Similar Threads

  1. Initialization and re-assignment of arrays
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 09:38 PM
  2. Replies: 0
    Last Post: 03-31-2008, 11:16 AM
  3. Icon initialization problem
    By saz25 in forum AWT / Swing
    Replies: 1
    Last Post: 12-24-2007, 11:37 PM
  4. Lazy Initialization
    By onegcr in forum New To Java
    Replies: 1
    Last Post: 08-14-2007, 04:29 PM
  5. log4j initialization
    By arfatkhan in forum Web Frameworks
    Replies: 3
    Last Post: 08-10-2007, 08:42 PM

Posting Permissions

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