enthuware.ocajp.i.v7.2.1131

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

Moderator: admin

Post Reply
ashishrai.kv
Posts: 33
Joined: Tue Jan 09, 2018 2:12 am
Contact:

enthuware.ocajp.i.v7.2.1131

Post by ashishrai.kv »

Code: Select all

What would be the result of attempting to compile and run the following program?

class TestClass{
   static TestClass ref;
   String[] arguments;
   public static void main(String args[]){
      ref = new TestClass();
      ref.func(args);
   }
   public void func(String[] args){
      ref.arguments = args;
   }
}

one of the incorrect answer explained is :-The concept here is that a non-static method (i.e. an instance method) can only be called on an instance of that class. Whether the caller itself is a static method or not, is immaterial. The main method is calling ref.func(); - this means the main method is calling a non-static method on an actual instance of the class TestClass (referred to by 'ref'). Hence, it is valid. It is not trying calling it directly such as func() or this.func(), in which case, it would have been invalid.


and correct answer for above problem is that it will compile as here ref is static and invoking func(String args) will not be any problem.

can you show examples or scenarios by modifying the above example only as when it will show an error while accessing the main method?

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

Re: enthuware.ocajp.i.v7.2.1131

Post by admin »

As the explanation says, you can change ref.func() to func() or this.func() to fail compilation.
If you like our products and services, please help us by posting your review here.

eyal1983
Posts: 3
Joined: Thu Oct 12, 2017 3:54 am
Contact:

Re: enthuware.ocajp.i.v7.2.1131

Post by eyal1983 »

I do not understand;

How can a static variable ("ref" - in this example) call a non-static method ("func")?

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

Re: enthuware.ocajp.i.v7.2.1131

Post by admin »

eyal1983 wrote:
Mon Jun 25, 2018 5:04 am
I do not understand;

How can a static variable ("ref" - in this example) call a non-static method ("func")?
You have asked a very basic question. You should go through a good book to understand the concept of static and instance before trying to make sense of this. But in short, whether ref is static or non-static is irrelevant. What is important is that ref is a reference variable that points to an object of TestClass. The method func is being invoked on this object.

Again, my suggestion is that you should not be attempting mock exams at this stage of your preparation because your fundamentals are too weak.
If you like our products and services, please help us by posting your review here.

eyal1983
Posts: 3
Joined: Thu Oct 12, 2017 3:54 am
Contact:

Re: enthuware.ocajp.i.v7.2.1131

Post by eyal1983 »

whether ref is static or non-static is irrelevant. What is important is that ref is a reference variable that points to an object of TestClass. The method func is being invoked on this object.
-Thanks!

Which book do you prefer?

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

Re: enthuware.ocajp.i.v7.2.1131

Post by admin »

You can go with either one of the two mentioned here: http://enthuware.com/index.php/mock-exa ... fication-8
If you like our products and services, please help us by posting your review here.

eyal1983
Posts: 3
Joined: Thu Oct 12, 2017 3:54 am
Contact:

Re: enthuware.ocajp.i.v7.2.1131

Post by eyal1983 »

OK 10x

Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests