PDA

View Full Version : I have a basic doubt - Java


Tiddler
09-11-2008, 04:18 PM
Hi,

I executed this program without Object and with Object.
With object my answer is 200,202, but I'm not using object ref. I guess,
It should be 18,19. What is the reason ?

// This is my program

package com.test;

public class TestOne {

private static int a=10;
private static int b;

static{
if(a ==10){
a=18;
b=19;
}else{
b=222;
}
}

public TestOne(){
a =200;
b =202 ;
}

public static void main(String[] args) {
TestOne t = new TestOne();
System.out.println(TestOne.a); // Calling without object REF.
System.out.println(TestOne.b);
}
}

shyam
09-12-2008, 12:28 PM
because you are calling the constructor which is re-initializing the values

public static void main(String[] args) {
System.out.println(TestOne.a); // Calling without object REF.
System.out.println(TestOne.b);
}

should give you the expected result