Page 1 of 1

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

Posted: Thu Nov 14, 2013 12:25 pm
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

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

Posted: Thu Nov 14, 2013 1:03 pm
by admin
That is exactly the reason.
HTH,
Paul.

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

Posted: Wed Jul 16, 2014 12:51 pm
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:

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

Posted: Wed Jul 16, 2014 7:49 pm
by admin
toString is invoked because of: System.out.println(this);

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

Posted: Thu Jul 31, 2014 11:15 am
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;

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

Posted: Thu Jul 31, 2014 11:31 am
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.

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

Posted: Sat Aug 02, 2014 2:50 am
by JvmEddy73
Hi paul,

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

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

Posted: Sun Feb 05, 2017 1:10 am
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?

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

Posted: Sun Feb 05, 2017 1:40 am
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)

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

Posted: Fri Apr 07, 2017 7:44 am
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

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

Posted: Fri Apr 07, 2017 9:04 am
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.

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

Posted: Fri Apr 07, 2017 1:44 pm
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!!

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

Posted: Fri Apr 07, 2017 9:05 pm
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.

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

Posted: Thu Nov 07, 2019 1:15 pm
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.

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

Posted: Thu Nov 07, 2019 10:51 pm
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.

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

Posted: Fri Nov 08, 2019 1:00 pm
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.

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

Posted: Wed Sep 15, 2021 10:10 am
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?

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

Posted: Wed Sep 15, 2021 11:55 am
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.