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

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

Moderator: admin

Post Reply
andgvoz
Posts: 3
Joined: Sat Nov 30, 2013 1:14 pm
Contact:

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

Post by andgvoz »

Hello! Can you help me with understanding the question.

What does it mean:
Incorrect answer:"All methods in a class are implicitly passed a 'this' parameter when called."
Explanation: "All non-static/instance methods in a class are implicitly passed a 'this' parameter when called."

Can you tell me, what does it mean "passed a 'this' parameter when called"? Thank you a lot!

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

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

Post by admin »

When you call a method, you pass in the parameters. For example, if you want to call a method m1 with some integer parameter, you can write - m1(10); or m1(x); Here, you are passing 10 or the variable x as a parameter.

Now, in case of instance methods, along with the parameter 10 or x, another parameter named this is also passed, which is not visible in the code, but can still be used.

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

andgvoz
Posts: 3
Joined: Sat Nov 30, 2013 1:14 pm
Contact:

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

Post by andgvoz »

Thanks a lot for your explanation!

Chandni
Posts: 6
Joined: Thu May 08, 2014 12:13 pm
Contact:

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

Post by Chandni »

You mean to say, this.methodName() can be used if its in the same class. If you create an instance if that class, you write instance.methodName() right ?

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

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

Post by admin »

No, we are talking about what you can do inside an instance method. Within an instance method you always have a reference named "this", which you can use to call methods on the same instance. Please read this: http://docs.oracle.com/javase/tutorial/ ... iskey.html
If you like our products and services, please help us by posting your review here.

Sergey
Posts: 39
Joined: Sat Jul 29, 2017 1:04 pm
Contact:

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

Post by Sergey »

admin wrote:Now, in case of instance methods, along with the parameter 10 or x, another parameter named this is also passed, which is not visible in the code, but can still be used.
Thanks, it is clear now

mihhay
Posts: 10
Joined: Wed May 09, 2018 12:48 pm
Contact:

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

Post by mihhay »

"Since there is no current object available for a static method, 'this' reference is not available in static methods and therefore it can only be used within instance methods. For the same reason, static methods cannot access non static fields or methods of that class directly i.e. without a reference to an instance of that class."

Can you please help me to understand what is in bold ? maybe with an example ?
Thank you !

LE: Reading on a site, I found out that :
"1) instance methods can also access static variables and static methods directly
2) methods static methods cannot access instance variables and instance methods directly; they need some object reference to do so"

Regarding 1), somehow I tough that only static can access static
Regarding 2) this means "c.countCars" ?

Code: Select all

class A{
static void countCars =0;
}

class B{
Car c = new Car();
[b]c.countCars[/b](){
etc }
}
Last edited by mihhay on Mon May 14, 2018 4:37 am, edited 1 time in total.

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

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

Post by admin »

Code: Select all

class X{
   int value = 10;
   public static void main(String[] args){
       System.out.println(this.value); //will not compile because main is static, 
//so "this" is not available in main

        X x = new X(); //creating an instance of X
        System.out.println(x.value); //this is ok because now you are accessing instance member value through a valid reference that points to an instance of X.
   }

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

pawel-klo
Posts: 3
Joined: Fri Mar 15, 2019 3:05 am
Contact:

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

Post by pawel-klo »

The following answer has been set as correct, which is not true:
Each object of a class has its own copy of each non-static member variable
As I understand Java, it should be stated:
each object of a class has access to each non-static member variable
.

Given below code confirms that:

Code: Select all

public class ExamTest{
   static int count = 0;

   public static void main(String[] args){

      ExamTest et1 = new ExamTest();
      ExamTest et2 = new ExamTest();   
   
      et1.count++;
      et2.count++;    
  
      System.out.println("static: " + count);
      System.out.println("et1 : " + et1.count);
      System.out.println("et2 : " + et2.count);
   }
}
output:
static: 2
et1 : 2
et2 : 2

We have here one single output, so there are no different copies of non-static variable. Please verify my thought.

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

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

Post by admin »

But count is not a non-static variable. Observe its declaration. It is a static variable.
If you like our products and services, please help us by posting your review here.

pawel-klo
Posts: 3
Joined: Fri Mar 15, 2019 3:05 am
Contact:

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

Post by pawel-klo »

right - sorry for this oversight

Post Reply

Who is online

Users browsing this forum: No registered users and 23 guests