Page 1 of 1

About Question enthuware.ocajp.i.v8.2.886 :

Posted: Mon Jun 15, 2015 3:09 pm
by berensn
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.

Re: About Question enthuware.ocajp.i.v8.2.886 :

Posted: Mon Jun 15, 2015 8:25 pm
by admin
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.

Re: About Question enthuware.ocajp.i.v8.2.886 :

Posted: Tue Jun 16, 2015 11:42 am
by berensn
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.

Re: About Question enthuware.ocajp.i.v8.2.886 :

Posted: Wed Oct 14, 2015 11:19 pm
by ssatyak
I understand the explanation for code lines 3 and 7. But what about lines 2 and 6?

Code: Select all

void m1(){
         m4();             // 2  --> calling a static method from non-static
}
static void m4(){ }
Similarly line 6:

Code: Select all

void m3(){
        m2();            // 6 --> calling a static method from non-static
    }
static void m2(){ } 
Can we call a static method/variable from non-static method? Pls explain.

Thank you
Satya

Re: About Question enthuware.ocajp.i.v8.2.886 :

Posted: Wed Oct 14, 2015 11:37 pm
by admin
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.

Re: About Question enthuware.ocajp.i.v8.2.886 :

Posted: Thu Oct 15, 2015 9:43 pm
by ssatyak
Thank you