This question is just wildly confusing and the explanation doesn't help.
It says if I want to call an instance method from an instance method I don't need a reference and 'this' is implicit. then it says if I'm calling an instance method from an instance method I need a reference. The explanation also seems to ignore that the 'this' keyword doesn't apply to static methods and fields.
About Question enthuware.ocajp.i.v8.2.886 :
Moderator: admin
-
- Posts: 4
- Joined: Sat Jun 13, 2015 3:08 pm
- Contact:
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.886 :
To call an instance method, you need a reference that points to the object on which you want to call that method. Now, within an instance method a reference named "this" pointing to the current object is always available. So to call another instance method from within an instance method, you can either use the this reference explicitly (for example, you may call this.m3() from within m1) , or leave out the this reference altogether (for example, you can directly call m3() from within m1) because the compiler automatically adds the "this." part implicitly. The "this" variable is available only within an instance method and not in static methods because static methods are not invoked within the context of an object of that class.
Please let me know if something is still not clear.
HTH,
Paul.
Please let me know if something is still not clear.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 4
- Joined: Sat Jun 13, 2015 3:08 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.886 :
I think I got it. You can call instance methods from within other instance methods by using object references, 'this' or neither but not by using the classname, which is what is being done on line 3 and 7. However if you are calling a static method you can use object reference, classname or neither but not by using 'this'; which is why line 1, StaticTest.m2(), is valid syntax.
-
- Posts: 2
- Joined: Wed Oct 14, 2015 11:15 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.886 :
I understand the explanation for code lines 3 and 7. But what about lines 2 and 6?
Similarly line 6:
Can we call a static method/variable from non-static method? Pls explain.
Thank you
Satya
Code: Select all
void m1(){
m4(); // 2 --> calling a static method from non-static
}
static void m4(){ }
Code: Select all
void m3(){
m2(); // 6 --> calling a static method from non-static
}
static void m2(){ }
Thank you
Satya
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v8.2.886 :
Yes, a static method or a static variable belongs to the class and not to a specific instance. Therefore, all instance of that class share the static members of that class and can call them with either an explicit class name such as TestClass.staticMethod() or without it. i.e. just staticMethod().
Java also allows instance methods to call static members using a reference variable of that class. i.e. testClassRef.staticMethod().
The question that you've asked is very basic. This tells me that you haven't really read any java book prior to attempting the mock exams. This is not advisable. You should first get the basics cleared by going through a regular java book and then attempting the mock exams. Otherwise, you are not making full use of the mock exams. It is up to you.
HTH,
Paul.
Java also allows instance methods to call static members using a reference variable of that class. i.e. testClassRef.staticMethod().
The question that you've asked is very basic. This tells me that you haven't really read any java book prior to attempting the mock exams. This is not advisable. You should first get the basics cleared by going through a regular java book and then attempting the mock exams. Otherwise, you are not making full use of the mock exams. It is up to you.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Wed Oct 14, 2015 11:15 pm
- Contact:
Who is online
Users browsing this forum: diogo_loureiro933 and 117 guests