Page 1 of 1

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

Posted: Sat Nov 30, 2013 1:18 pm
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!

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

Posted: Sat Nov 30, 2013 8:55 pm
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.

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

Posted: Sun Dec 01, 2013 2:56 am
by andgvoz
Thanks a lot for your explanation!

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

Posted: Thu May 08, 2014 12:17 pm
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 ?

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

Posted: Thu May 08, 2014 10:07 pm
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

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

Posted: Fri Sep 22, 2017 2:55 pm
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

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

Posted: Mon May 14, 2018 2:32 am
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 }
}

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

Posted: Mon May 14, 2018 3:29 am
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.
   }

}

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

Posted: Fri Mar 15, 2019 3:19 am
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.

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

Posted: Fri Mar 15, 2019 3:42 am
by admin
But count is not a non-static variable. Observe its declaration. It is a static variable.

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

Posted: Sat Mar 16, 2019 1:41 am
by pawel-klo
right - sorry for this oversight