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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

ETS User

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

Post by ETS User »

Where does "int x;" gets default value?

admin
Site Admin
Posts: 9425
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

ETS User

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

Post by ETS User »

Tnx a lot!

ETS user

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

Post 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?

admin
Site Admin
Posts: 9425
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Wisevolk
Posts: 33
Joined: Thu Jul 18, 2013 6:06 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 9425
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

This is out of scope for the exam, but if you like to learn more, please see this.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

convertor
Posts: 7
Joined: Mon Jan 27, 2014 6:25 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 9425
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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
If you like our products and services, please help us by posting your review here.

fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

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

Post by fasty23 »

Explanation explained a complicated subject perfectly, that I've never seen in any book before.
Thank you.

crux terminatus
Posts: 12
Joined: Sat Feb 22, 2014 3:55 pm
Contact:

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

Post 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 :))

admin
Site Admin
Posts: 9425
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

sarakh
Posts: 23
Joined: Fri Jun 20, 2014 3:12 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 9425
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

sarakh
Posts: 23
Joined: Fri Jun 20, 2014 3:12 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 9425
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

alkour
Posts: 30
Joined: Tue Mar 24, 2015 2:59 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 9425
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

MUNISH JASWAL
Posts: 2
Joined: Tue Apr 05, 2016 7:14 am
Contact:

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

Post by MUNISH JASWAL »

static int i2 ;
............
{ j = 30; i2 = 40; }

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

admin
Site Admin
Posts: 9425
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

What happened when you tried it out?
If you like our products and services, please help us by posting your review here.

raj_dp
Posts: 16
Joined: Sun Jun 12, 2016 7:43 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 9425
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

VijayTripathi
Posts: 8
Joined: Mon Jul 06, 2020 1:11 pm
Contact:

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

Post 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

admin
Site Admin
Posts: 9425
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

VijayTripathi
Posts: 8
Joined: Mon Jul 06, 2020 1:11 pm
Contact:

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

Post 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?

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests