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

    Default initialization value problem


    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);
    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
    Rep Power

    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
    Rep Power


    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;
    		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, 08:38 PM
  2. Replies: 0
    Last Post: 03-31-2008, 10: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, 03:29 PM
  5. log4j initialization
    By arfatkhan in forum Web Frameworks
    Replies: 3
    Last Post: 08-10-2007, 07: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