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

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

Moderator: admin

Post Reply
lordnovas
Posts: 12
Joined: Tue Jan 15, 2013 3:31 pm
Contact:

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

Post by lordnovas »

Hey Guys,

I'm not sure what the answer notes are talking about when they say the following:
x is primitive int.You cannot call any methods on a primitive. so this.x.setInt(...) or this.y.setInt(...) don't make any sense.
I know the way they have created the "setInt" method is weird but from the looks of it they are simply creating a method within the "setValues" method and then setting the primitive values of the instance variables x & y to equal whatever int is passed to the method. However they say that you can't call any methods on a primitive so I'm a bit confused. Please help

The Code in question that's calling a method on a primitive is below:
//add the following method to data class:

Code: Select all

 public void setValues(int x, int y){   
this.x.setInt(x);   this.y.setInt(y); }  

//Then add the following statement: 

d.setValues(2, 2);
This is the entire code for the question:

Code: Select all

class Data {
    private int x = 0, y = 0;
    public Data(int x, int y){
        this.x = x; this.y = y;
    }
}
public class TestClass {
    public static void main(String[] args) throws Exception {
        Data d = new Data(1, 1);
        //add code here
    }
}
Which of the following options when applied individually will change the Data object referred to by the variable d to contain 2, 2 as values for its data fields?

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

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

Post by admin »

I am not sure I understand your question correctly, specially the part where you say, "they are simply creating a method within the "setValues" method" because you can't create method within a method in Java.

By "You cannot call any methods on a primitive.", it means exactly that. Primitives do not have any methods. So you can't call methods on primitive like you do for objects. Therefore, this.x.setInt(x);   this.y.setInt(y); are invalid because x and y are primitive ints. They don't have any methods.

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

lordnovas
Posts: 12
Joined: Tue Jan 15, 2013 3:31 pm
Contact:

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

Post by lordnovas »

Ohhhhh, sorry for being a bit confusing but you hit what I was getting at. Basically all objects like String, ArrayList, or whatever I create have methods already. However Primitives don't have any methods because their not objects. I guess I was over thinking the problem or something.

Thanks for the perspective.

Cheers
j-

aditya
Posts: 3
Joined: Sun Jul 27, 2014 6:20 am
Contact:

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

Post by aditya »

Hi Admin
can you please explain in details why
d = new Data(2, 2);
option above is wrong.?

my understanding says : by using d = new Data(2, 2); we actually now tell d object to call Data constructor and set respective values.
Please correct me where i am missing. :?

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

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

Post by admin »

Did you read the explanation provided with this option? It says, "This will create a new Data object and will not change the original Data object referred to be d."

The question asks you to change the values inside the Data object already created in the code. It doesn't want you to create a new Data object.
If you like our products and services, please help us by posting your review here.

enthunoob
Posts: 60
Joined: Thu Apr 15, 2021 12:21 pm
Contact:

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

Post by enthunoob »

I think option 5 wouldn't point to the same object anyway as it would require to recompile the project. So as d will be pointing to a different object anyway, I prefer option 2 :D

Post Reply

Who is online

Users browsing this forum: No registered users and 36 guests