About Question enthuware.ocajp.i.v7.2.1308 :

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

Moderator: admin

Post Reply
Zoryanat
Posts: 20
Joined: Tue Aug 27, 2013 3:16 am
Contact:

About Question enthuware.ocajp.i.v7.2.1308 :

Post by Zoryanat »

Hi there.

Could you please help me understand why answer is "It will print TestClass.si = 10;"?
I mean, TestClass wouldn't be initialized yet by the moment we try to print its static variable si.

Is that because si is static and thus belongs to class and doesnt need to wait for instance to be initialized or is there other reason that i am missing?

Many thanks!
Regards
Zoryana

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

Re: About Question enthuware.ocajp.i.v7.2.1308 :

Post by admin »

That is exactly the reason.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Nisim123
Posts: 42
Joined: Mon Jan 20, 2014 2:26 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1308 :

Post by Nisim123 »

Ok till now it might be understood that since the si member variable is declared static,
it is printed whenever you use the 'this' keyword with the System.out.print();
But the question is what the heck invokes the :
public String toString(){       return "TestClass.si = "+this.si;    }

method which is not static, and there is no code that calls it
and it does not appear in the assignment operation of the si member variable.
In other words I would have expected that at least there would be something like:

static String siOfTestClass = toString();

that will produce this output. :evil:

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

Re: About Question enthuware.ocajp.i.v7.2.1308 :

Post by admin »

toString is invoked because of: System.out.println(this);
If you like our products and services, please help us by posting your review here.

JvmEddy73
Posts: 2
Joined: Thu Jul 31, 2014 11:01 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1308 :

Post by JvmEddy73 »

Hi,

When i run the code like this (as suggested in your exercise):

 

Code: Select all

System.out.println(this);

it prints the hashcode.

but when i do like this :

 

Code: Select all

System.out.println(this.tosString());


It works and prints "TestClass.si = 10"

So, for some reasons, the this keyword is not calling the toString method.

I'm using eclipse with Java 7;

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

Re: About Question enthuware.ocajp.i.v7.2.1308 :

Post by admin »

I see there is a typo in the line of code that you've pasted (You have this.tosString instead of this.toString). Please post the exact and complete code that you are trying to run.

-Paul.
If you like our products and services, please help us by posting your review here.

JvmEddy73
Posts: 2
Joined: Thu Jul 31, 2014 11:01 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1308 :

Post by JvmEddy73 »

Hi paul,

You are right.
I'm ashamed. :oops:
My flat excuses.

Deleted User 3513

Re: About Question enthuware.ocajp.i.v7.2.1308 :

Post by Deleted User 3513 »

admin wrote:toString is invoked because of: System.out.println(this);
Is(.toString()) it always invoked when System.out.println() method is used?

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

Re: About Question enthuware.ocajp.i.v7.2.1308 :

Post by admin »

Only if the reference passed to the println method is not null.
See this: https://docs.oracle.com/javase/7/docs/a ... ng.Object)
and then https://docs.oracle.com/javase/7/docs/a ... ng.String)
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1308 :

Post by Javier »

Hi Paul!

I have one doubt about this test:

Code: Select all

public class TestClass{
   static int si = 10;
   public static void main (String args[]){
      new TestClass();
   }
   public TestClass(){
      System.out.println(this);
   }
   public String toString(){
      return "TestClass.si = "+this.si;
   }
}
what does it mean the keyword "this" inside the code:

Code: Select all

public TestClass(){
      System.out.println(this);
}
is it a reference to the static variable is?? or is a reference to the object of new TestClass()??


Thank you very much Paul
Last edited by admin on Fri Apr 07, 2017 8:58 am, edited 1 time in total.
Reason: Please enter code inside [code] [/code] tags

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

Re: About Question enthuware.ocajp.i.v7.2.1308 :

Post by admin »

No, "this" is always a reference to the current object of current class. See this: http://javabeginnerstutorial.com/core-j ... d-in-java/
and http://stackoverflow.com/questions/3728 ... is-in-java

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Javier
Posts: 66
Joined: Mon Feb 20, 2017 12:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1308 :

Post by Javier »

Hi Paul!

If I understood correct, in the code:

public TestClass(){
System.out.println(this);
}

"this" is a reference to the entire object created by:

public static void main (String args[]){
new TestClass();
}

am I right Paul?

Thank you!!

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

Re: About Question enthuware.ocajp.i.v7.2.1308 :

Post by admin »

In this case that is correct because that is the only TestClass object being created but this is not the right way to look at it.
"this", inside the constructor, refers to the object for which the constructor is being executed.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

DazedTurtle
Posts: 26
Joined: Wed Oct 02, 2019 1:42 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1308 :

Post by DazedTurtle »

Do you have any advice on how to remember that non-static calling static is fine, but it's the other way around that's bad? Because this is the second time I've gotten a question of this type wrong because I keep reversing which one is bad and which one is good.

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

Re: About Question enthuware.ocajp.i.v7.2.1308 :

Post by admin »

Well, no advice is needed if you remember the basic concept that instance methods can only be invoked on an instance using a reference to that instance. If you don't have a reference that points to an instance, you can't invoke an instance method.

static methods do not require a reference to an instance to be invoked. Inside a static method the implicit variable 'this' that points to the current instance is not available so you can't invoke an instance method from a static method without a valid reference to an instance.

I would suggest you to go through a good book to learn these basic concept first before attempting the mock exams. I recommend OCP Java 11 Fundamentals by Hanumant Deshmukh. It covers all this.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

DazedTurtle
Posts: 26
Joined: Wed Oct 02, 2019 1:42 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1308 :

Post by DazedTurtle »

I did read the book. Apparently, despite good grades, I just never learned how to actually study (or I've forgotten how to since I graduated), and it feels like I'm taking a cumulative final exam in a class that didn't have any previous tests of any kind and that only had a couple homework assignments.

I understand (most of) everything just fine. The problem is that apparently I'm not absorbing the knowledge.

It's honestly a little demoralizing because I did well in classes, and I'm more comfortable with Java than with any other language, but then I take these tests and suddenly it turns out I have all these little gaps in my knowledge that are fine by themselves, but keep adding up until I'm right on the threshold between passing and failing.

On the plus side, at least I didn't have to spend $245 to learn this lesson, thanks to the mock exams.

Edit: Reflecting further, I think I'm also so focused on "don't forget anything!" that I'm trying to memorize things instead of the concepts behind the things. Which means I've been going against the entire point of the exam.

So that actually helps a lot, because now that I know what I'm doing wrong, I can refocus my efforts on doing it right.

qazwsx922
Posts: 7
Joined: Sat Sep 04, 2021 8:17 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1308 :

Post by qazwsx922 »

I am trying to understand that System.out.println(this);will call toString method so return"TestClass.si = "+ this.si;

I also tried changing

Code: Select all

System.out.println(this);
to

Code: Select all

System.out.println(si);
and to

Code: Select all

     System.out.println(TestClass.si);
. And I saw both of them just print 10.

I read the link you wrote in above answer (https://stackoverflow.com/questions/372 ... is-in-java), (https://docs.oracle.com/javase/7/docs/a ... .Object%29)
what I understood there is that 'this' refers to this entire object.

'this' refers to the current object,
and when inside

Code: Select all

System.out.println()
object is being passed, toString method will be called. If there's no toString method written by programmer, then it will print 'ClassName@hashcode' (TestClass@2401f4c3) and hashcode is memory location?

in this case because 'this' is passed, toString will be called so print TestClass.si = 10 . Did I understand correctly?

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

Re: About Question enthuware.ocajp.i.v7.2.1308 :

Post by admin »

java.lang.Object class (which is the ultimate parent of all classes in Java) has a toString method which generates a String of the form ClassName@hashcode (eg TestClass@2401f4c3). Hashcode is hash code. It is not a memory location.

So, when you pass any reference to the println() method, internally, the toString() is invoked on the object whose reference you have passed and whatever String is returned by toString, that String is printed.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 43 guests