Page 1 of 2

About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Mon Nov 12, 2012 10:48 am
by ETS User
Where does "int x;" gets default value?

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Mon Nov 12, 2012 3:12 pm
by admin
int x; is an instance member of the class. Therefore, when an object of this class is created, x is assigned 0 as a default value.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Tue Nov 13, 2012 3:51 am
by ETS User
Tnx a lot!

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Wed Dec 12, 2012 11:41 pm
by ETS user
Re: the example in the explanation - since i is static, A1.i is accessible and by the same token, even if a1 is null, you can still access a1.i. But I'm confused about the "A1 Loaded" message - is it being printed because this is the first usage of a1?

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Thu Dec 13, 2012 7:36 am
by admin
Yes, as soon as you access ai.i, you are making use of class A1 and so it's static initialiazer will be execute.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Sat Jul 27, 2013 6:09 am
by Wisevolk

Code: Select all

public class InitClass {

    public static void main(String[] args) {
              InitClass obj = new InitClass(5);
    }
        
    {j=30;i2=40;}
    int m;
    static int i1=5;
    static int i2;
    int j = 100;
    int x;
    public InitClass(int m){
        System.out.println(i1+"/"+i2+"/"+x+"/"+j+"/"+m);
    }
    
    static{i1++;}
}
Hello,
Can you explain why if I put the instance block before all variables declaration I can compile and run (j will be 100).
If I try with the static block I can't, I understand that because they are executed in the order they have been wrote, but for instance's blocks are the variables read before the blocks even if they are after the block in the code and then why I have a value of 100 to j ?
Thanks

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Sat Jul 27, 2013 6:17 am
by admin
This is out of scope for the exam, but if you like to learn more, please see this.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Sat Feb 01, 2014 9:18 pm
by convertor
admin wrote:int x; is an instance member of the class. Therefore, when an object of this class is created, x is assigned 0 as a default value.

HTH,
Paul.
it is assigned before the Construction method is complete?

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Sun Feb 02, 2014 9:03 pm
by admin
Yes, fields are always initialized to default values first. A constructor may change the default values later. Please see this: http://docs.oracle.com/javase/specs/jls ... jls-4.12.5

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Tue Feb 25, 2014 12:26 am
by fasty23
Explanation explained a complicated subject perfectly, that I've never seen in any book before.
Thank you.

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Sat Mar 01, 2014 12:21 pm
by crux terminatus
Are static initializers always executed before instance initializers, regardless of their order relative to each other in the code?

Can static initializers access non-static variables? (Guess: no :))

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Sat Mar 01, 2014 10:07 pm
by admin
Yes, because a class has to be loaded before an instance of that can be created.
No, because you need an instance to access instance variable.

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Tue Jun 24, 2014 4:39 pm
by sarakh
I have three questions here:

1. So "public class InitClass" has a "int m"
And it also has a constructor
public InitClass(int m){
System.out.println(i1 + " " + i2 + " " + x + " " + j + " " + m);
}
Why is it that when we say
InitClass obj = new InitClass(5);
m is assigned to 5?
The constructor never does that assigning explicitly, so I thought that by new InitClass(5); a copy of int m is passed to the class and the actual value of int m is not changed. What does it mean when we say "m is shadowed?"

2. What are the "Static Initializer"s and what is the difference between a "Static Initializer" and a "Instance Initializer"? Which one is performed first?

3. In the explanation, "A Loaded" is printed before anything else, although we never tried to create an instance of class A in the main method ? does that mean that the "Static Initializer" is performed regardless of creating an instance of that class?

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Tue Jun 24, 2014 8:01 pm
by admin
1. This is how argument passing works. m is a parameter, 5 is an argument. When you call new InitClass(5), the constructor doesn't assign 5 to m. The JVM does. The JVM assigns 5 to m while invoking the constructor. I am not sure where are you getting the copy of m. m doesn't even exist outside the definition of the constructor. What is the "actual" value of int m that you are talking about?

2. This should help http://www.engfers.com/code/static-initializer-block/ or http://www.jusfortechies.com/java/core- ... blocks.php

3. Once you understand the above, this will be clear.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Wed Jun 25, 2014 5:00 am
by sarakh
Very interesting!
So when we have

Code: Select all

public class InitClass{    
   int m;
   public InitClass(int j){
      System.out.println("m: " + m);   }   
    public static void main(String args[ ] ){
      InitClass obj = new InitClass(5);   }
}
The out put is: m:0

But when we have

Code: Select all

public class InitClass{
   int m;
   public InitClass(int m){
      System.out.println("m: " + m);  }  
    public static void main(String args[ ] ){
      InitClass obj = new InitClass(5);   }
}
The out put is: m:5

And thanks for the link about static initialisers!
Wow! :geek:
I think I understood it pretty fine.
Is this in the scope of the 803 exam though?

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Wed Jun 25, 2014 5:21 am
by admin
sarakh wrote: Is this in the scope of the 803 exam though?
It is really difficult to answer this question because it doesn't fall directly under any official objective but then even creating a new object using constructors doesn't fall under any objective.

I could say no, it is not required, but you might very well get a question on it :(

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Fri Apr 10, 2015 12:32 pm
by alkour
Hi Paul,

I am a bit confused with the statement in the explanation to A, saying that static member could be initialized in the instance initializer block.

Is it correct? Could you comment it.

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Fri Apr 10, 2015 10:10 pm
by admin
I don't see any such explanation to option A in this question.
In general though, it is possible for an instance initializer to access a static member and assign some value to it.

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Sat Apr 16, 2016 9:12 pm
by MUNISH JASWAL
static int i2 ;
............
{ j = 30; i2 = 40; }

Can we initialize(assign value) a static variable i2 inside an instance block?

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Sat Apr 16, 2016 11:27 pm
by admin
What happened when you tried it out?

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Thu Dec 06, 2018 12:49 am
by raj_dp
In question number 16, when we accessed the static String ID = "QBANK" through Sub class reference,
the Sub class's static block did not get executed.
But in this problem under Explanation you have taken one example where there are two classes A1 and
A. The class A1 has a static variable (static int i = 10;) and a static block.
Here also the static variable is accessed by using the class reference:
System.out.println(a1.i);
But here the class is loaded and the static block is executed. We are using the class reference
here as: A1 a = null; where we are not even creating an object of the class.
Can you please explain me the difference between the two.

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Thu Dec 06, 2018 1:52 am
by admin
A1 is loaded when you do a1.i not when you do A1 a = null; Since i is defined in A1, how will you get its value if you don't load class A1 ??

The difference in the other question was that the variable was defined in the super class and you were using reference of subclass. That is not the case here.

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Tue Aug 25, 2020 11:11 am
by VijayTripathi
I am unable to understand from the previous explanations.
Can you please tell from the explanation where a1 is assigned to null.
A1 a1 = null;
System.out.println(" A1 should not have been loaded");// it shows that A1 is not loaded yet

Question 1. is why it has not given NullPointerException as accessing from null.
If a1 is in existence as we are calling variable i using a1, than why it has not assigned null to it and subsequently giving the NullPointerException.

Thanks in advance

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Tue Aug 25, 2020 12:10 pm
by admin
i is a static variable. Access to static variable is bound by the compiler at compile time. The compiler does not try to access the reference variable a1. The compiler knows that the type of a1 is is A1 and so, basically, it changes the code from a1.i to A1.i. That is why there is no NPE.

Re: About Question enthuware.ocajp.i.v7.2.1251 :

Posted: Thu Aug 27, 2020 1:22 am
by VijayTripathi
admin wrote:
Tue Aug 25, 2020 12:10 pm
i is a static variable. Access to static variable is bound by the compiler at compile time. The compiler does not try to access the reference variable a1. The compiler knows that the type of a1 is is A1 and so, basically, it changes the code from a1.i to A1.i. That is why there is no NPE.
wonderful. So does it happen with the static methods as well?