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

Thread: Why the value of ' i' is 0;

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Why the value of ' i' is 0;

    Hi ! I'm new here.I have a question.I don't konw why the value of ' i' is 0;And the following is my code. Thx.


    public class TestDemo {
    public static void main(String[] args) {
    new Son();
    }
    }

    class Son extends Base {
    private int i = 22;
    public Son() {
    i = 222;
    }
    public void display() {
    System.out.println("i==="+i);
    }
    }

    class Base {
    private int i = 2;
    public Base() {
    this.display();
    }
    public void display() {
    System.out.println("i==="+i);
    }
    }

  2. #2
    Junior Member
    Join Date
    Sep 2013
    Location
    Denmark
    Posts
    27
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Why the value of ' i' is 0;

    1. First use f@?#!€% code block
    2. Take a look a this:
    56347d1b:java tools$ java test
      I base: 2
      2 Base: 0
      i===0
      i: 222
    56347d1b:java tools$
    public class Base {
    	private int i = 2;
    	public Base() {
    		System.out.println("I base: " + i);
    		this.display();
    	}
    	public void display() {
    		System.out.println("i==="+i);
    	}
    }
    public class Son extends Base {
    	private int i = 22;
    	public Son() {
    		i = 222;
    		System.out.println("i: " + i);
    	}
     
    	public void display() {
    				System.out.println("2 Base: " + i);
    		System.out.println("i==="+i);
    	}
    }
    public class test {
    	public static void main(String[] args) {
    	 new Son();
    	}
    }
    Follow the system.out.println calls and you might find something suprising
    Ruby 'cause I can - Java 'cause I want - C# 'cause I have to

  3. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Why the value of ' i' is 0;

    public class Son extends Base {
    	private int i = 22;    //12
    	{
    		System.out.println("private int i = "+i);   //13
    	}
    	public Son() {    //3
    		i = 222;    //14
    		System.out.println("i: " + i);    //15
    	}
     
    	public void display() {    //9
    		System.out.println("2 Base: " + i);    //10
    		System.out.println("i==="+i);    //11
    	}
    }

    public class Base {
    	private int i = 2;    //5
    	{
    		System.out.println("private int i = "+i);    //6
    	}
    	public Base() {    //4
    		System.out.println("I base: " + i);    //7
    		this.display();    //8
    	}
    	public void display() {
    		System.out.println("i==="+i);
    	}
    }

    public class Test {
    	public static void main(String[] args) {    //1
    	 new Son();    //2
    	}
    }

    private int i = 2
    I base: 2
    2 Base: 0
    i===0
    private int i = 22
    i: 222

    When and where was the 'i' defined before printing in "System.out.println("2 Base: " + i);"?
    It is a default value.

    --- Update ---

    Ha,ha.I got it.They have been defined when we creat an instance.But they haven't been initialized.That's why the value of 'i' is 0.Right?