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

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

Moderator: admin

Post Reply
berensn
Posts: 4
Joined: Sat Jun 13, 2015 3:08 pm
Contact:

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

Post 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.

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

berensn
Posts: 4
Joined: Sat Jun 13, 2015 3:08 pm
Contact:

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

Post 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.

ssatyak
Posts: 2
Joined: Wed Oct 14, 2015 11:15 pm
Contact:

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

Post 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

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

ssatyak
Posts: 2
Joined: Wed Oct 14, 2015 11:15 pm
Contact:

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

Post by ssatyak »

Thank you

Post Reply

Who is online

Users browsing this forum: No registered users and 111 guests