About Question enthuware.ocajp.i.v7.2.1251 :
Moderator: admin
Online
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1251 :
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?
Online
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 33
- Joined: Thu Jul 18, 2013 6:06 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
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++;}
}
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
Online
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Mon Jan 27, 2014 6:25 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
it is assigned before the Construction method is complete?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.
Online
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
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.
-
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
Explanation explained a complicated subject perfectly, that I've never seen in any book before.
Thank you.
Thank you.
-
- Posts: 12
- Joined: Sat Feb 22, 2014 3:55 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
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 )
Can static initializers access non-static variables? (Guess: no )
Online
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
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.
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.
-
- Posts: 23
- Joined: Fri Jun 20, 2014 3:12 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
I have three questions here:
1. So "public class InitClass" has a "int m"
And it also has a constructor
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?
1. So "public class InitClass" has a "int m"
And it also has a constructor
Why is it that when we saypublic InitClass(int m){
System.out.println(i1 + " " + i2 + " " + x + " " + j + " " + m);
}
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?
Online
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
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.
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.
-
- Posts: 23
- Joined: Fri Jun 20, 2014 3:12 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
Very interesting!
So when we have
The out put is: m:0
But when we have
The out put is: m:5
And thanks for the link about static initialisers!
Wow!
I think I understood it pretty fine.
Is this in the scope of the 803 exam though?
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); }
}
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); }
}
And thanks for the link about static initialisers!
Wow!
I think I understood it pretty fine.
Is this in the scope of the 803 exam though?
Online
I could say no, it is not required, but you might very well get a question on it
HTH,
Paul.
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
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.sarakh wrote: Is this in the scope of the 803 exam though?
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.
-
- Posts: 30
- Joined: Tue Mar 24, 2015 2:59 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
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.
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.
Online
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
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.
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.
-
- Posts: 2
- Joined: Tue Apr 05, 2016 7:14 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
static int i2 ;
............
{ j = 30; i2 = 40; }
Can we initialize(assign value) a static variable i2 inside an instance block?
............
{ j = 30; i2 = 40; }
Can we initialize(assign value) a static variable i2 inside an instance block?
Online
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
What happened when you tried it out?
If you like our products and services, please help us by posting your review here.
-
- Posts: 16
- Joined: Sun Jun 12, 2016 7:43 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
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.
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.
Online
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
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.
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.
-
- Posts: 8
- Joined: Mon Jul 06, 2020 1:11 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
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
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
Online
-
- Site Admin
- Posts: 10090
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
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.
-
- Posts: 8
- Joined: Mon Jul 06, 2020 1:11 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1251 :
wonderful. So does it happen with the static methods as well?admin wrote: ↑Tue Aug 25, 2020 12:10 pmi 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.
Who is online
Users browsing this forum: No registered users and 31 guests