About Question enthuware.ocajp.i.v7.2.1331 :
Moderator: admin
-
- Posts: 53
- Joined: Thu Nov 22, 2012 12:01 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1331 :
I don't exactly know what the 4th option means (i.e., c = AccessTest.this.a;). I thought AccessTest.this.a was the instance member a of class Inner. Can any one explain this?
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1331 :
AccessTest.this refers to the outer class object. Within a non-static inner class, "this" refers to the inner class object and "outerclassname.this" refers to the outer class instance.
So that means, AccessTest.this.a refers to the instance member a of AccessTest.
HTH,
Paul.
So that means, AccessTest.this.a refers to the instance member a of AccessTest.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 53
- Joined: Thu Nov 22, 2012 12:01 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1331 :
got it. Thank u.
-
- Posts: 33
- Joined: Mon May 06, 2013 9:41 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1331 :
Although it is not a very tough question, it deals with inner classes, which are not part of the OCAJP exam, as much as I am aware, so I wonder why it is still part of the question bank?
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1331 :
Some candidates have reported getting a questions with references to inner classes. So we have kept a few questions to avoid surprises in the exam.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 18
- Joined: Mon Jan 13, 2014 5:39 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1331 :
I really wish these quizzes would stick to questions that will be on the exam.
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1331 :
There are really only very few questions like to that in the he question bank and all are clearly marked as such. We have kept them because we believe they are important.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Tue Nov 29, 2016 10:31 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1331 :
Hello,
Can you tell me why point 5 is correct?
Br,
Pawel
Can you tell me why point 5 is correct?
Br,
Pawel
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1331 :
c = ""+b; <-- This makes c point to "x" because "" is empty and b points to 'x'.Qusite wrote:Hello,
Can you tell me why point 5 is correct?
Br,
Pawel
return c; <-- This returns c
Thus, the call to new Inner().get(), will return a String reference that points to a String containing "x", which is then printed.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 22
- Joined: Sun Jan 15, 2017 10:51 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1331 :
Paul, I believe b actually points to 'x' (single quotes instead of double quotes).
Thus,
results in c = "x" because the JVM converts what b points to into a string according to the rule of string concatenation specified here: https://docs.oracle.com/javase/tutorial ... s/op1.html
Thus,
Code: Select all
c = ""+b;
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1331 :
Yes, you are right. b points to 'x'. ""+'x' will be converted to "x".
If you like our products and services, please help us by posting your review here.
-
- Posts: 7
- Joined: Thu Mar 30, 2017 7:15 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1331 :
Note: This question may be considered too advanced for this exam.
Which statements can be inserted at line 1 in the following code to make the program write x on the standard output when run?
c = ""+b;
How come you can call static variable directly without using class name (it should be c = AccessTest.b; like this calling by using class name?)? and what "" this means is it a predefined syntax and if yes how it functions?
//Totally new to this topic please helpout
Which statements can be inserted at line 1 in the following code to make the program write x on the standard output when run?
Code: Select all
public class AccessTest{
String a = "x";
static char b = 'x';
String c = "x";
class Inner{
String a = "y";
String get(){
String c = "temp";
// Line 1
return c;
}
}
AccessTest() {
System.out.println( new Inner().get() );
}
public static void main(String args[]) { new AccessTest(); }
}
How come you can call static variable directly without using class name (it should be c = AccessTest.b; like this calling by using class name?)? and what "" this means is it a predefined syntax and if yes how it functions?
//Totally new to this topic please helpout
Last edited by admin on Sun Apr 16, 2017 5:52 am, edited 1 time in total.
Reason: Please enter code inside [code] [/code] tags
Reason: Please enter code inside [code] [/code] tags
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1331 :
Java language allows you to access a static member of a class using the class name syntax (i.e. AccessTest.b ) as well as through any reference variable whose declared type is of that class.Chandu wrote: How come you can call static variable directly without using class name (it should be c = AccessTest.b; like this calling by using class name?)? and what "" this means is it a predefined syntax and if yes how it functions?
//Totally new to this topic please helpout
Now, recall that "this" is a reference variable whose type is same as that of the current class. Thus, c = ""+b; is actually same as c = ""+this.b; and is therefore a valid way to access a static member b of class AccessTest.
It should be noted that accessing a static member using this or a reference variable is not a good practice even though it is legal. Using the class name is the preferred way because it is clear.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1331 :
Since last comment about if this can be on the exam is 5yrs old, my question is if nowdays this kind of question about inner classes can occur in the exam?
-
- Site Admin
- Posts: 10043
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1331 :
Cannot guarantee but mostly likely not on the exam.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: Bing [Bot] and 33 guests