Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_java thread: HELP ME PLEASE


Message #1 by "Muhammad Atif Ibrahim" <matifibrahim@y...> on Wed, 4 Jul 2001 18:44:22
The code from Test4 "Constructor" is executed after code from 
NewClass "Constructor".
When the object are created the variables "a" have initial values to 0
You will see the values 2 and 4 after the initialisation of object was 
made. In this exemple NewClass was built before Test4. The value a from 
NewClass was setted when construstor of NewClass ran the code, but the 
value from Test4 was not setted becouse all values of this object had not 
been setted with default values.

> WELL, IT DOES NOT SEEMS TO BE A DIFFICULT QUESTION. BUT THIS QUESTION IS 
> YET TO BE ANSWERED IN JAVA BEGINNERS LIST. THAT IS WHY I HAVE LAUNCHED 
IT 
> IN JAVA ADVANCE LIST. LOOK AT THE FOLLOWING CODE...
> 
> public class Test4 extends NewClass
> {
>     public int a=4;
>     public void add()
>      {
>         System.out.println("Entering in add() of Test4");
> 	super.add();
>         System.out.println(a);
>     	System.out.println("Leaving  add() of Test4");
>      } 
> 
>     Test4()
>      {
<---here by default exist the constructor Test4() {super();}
<--- here Test4.a = 4 and NewClass.a = 2
> 	System.out.println(a);
>      }
>     public static void main (String args[])
>      {
> 	Test4 obj1 = new Test4(); 
> 
> 	System.out.println("");
>      }
> }
> class NewClass
> {
>    public int a=2;
>    NewClass()
>     {
<---here Test4.a = 0 and NewClass.a = 2
>       add();
>       System.out.println(a);
>     }
>    public void add()
>     {
>       System.out.println(a); 
>       System.out.println("In add() of NewClass");
>     }
> }
> 
> THIS PROGRAM PRODUCES THE FOLLOWING OUTPUT ON SCREEN
> 
> OUTPUT
> ======
> 
> Entering in add() of Test4
> 2
> In add() of NewClass
> 0                       //WHY ZERO IS PRINTING HERE?
> Leaving add() of Test4
> 2
> 4
> 
> IN MY CALCULATION THERE SHOULD BE 4 OR 2 IN PLACE OF ZERO.
> I AM NOT ABLE TO UNDERSTAND THAT WHY ZERO IS PRINTING HERE.
> TELL ME PLEASE WHAT MISTAKE I AM MAKING OR WHAT RULE I AM VIOLATING 
> SO I CAN GET AWAY NEXT TIME WITH THESE KIND OF PROBLEMS. TELL ME
> IN DETAIL. I WILL BE THANKFUL

  Return to Index