About Question enthuware.ocajp.i.v7.2.1110 :
Moderator: admin
-
- Posts: 3
- Joined: Sat Nov 30, 2013 1:14 pm
- Contact:
About Question enthuware.ocajp.i.v7.2.1110 :
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!
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!
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1110 :
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.
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.
-
- Posts: 3
- Joined: Sat Nov 30, 2013 1:14 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1110 :
Thanks a lot for your explanation!
-
- Posts: 6
- Joined: Thu May 08, 2014 12:13 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1110 :
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 ?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1110 :
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.
-
- Posts: 39
- Joined: Sat Jul 29, 2017 1:04 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1110 :
Thanks, it is clear nowadmin 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.
-
- Posts: 10
- Joined: Wed May 09, 2018 12:48 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1110 :
"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" ?
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.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1110 :
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.
-
- Posts: 3
- Joined: Fri Mar 15, 2019 3:05 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1110 :
The following answer has been set as correct, which is not true:
Given below code confirms that:
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.
As I understand Java, it should be stated:Each object of a class has its own copy of each non-static member variable
.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);
}
}
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.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1110 :
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.
-
- Posts: 3
- Joined: Fri Mar 15, 2019 3:05 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1110 :
right - sorry for this oversight
Who is online
Users browsing this forum: Bing [Bot] and 52 guests