About Question enthuware.ocajp.i.v7.2.1308 :
Moderator: admin
-
- Posts: 20
- Joined: Tue Aug 27, 2013 3:16 am
- Contact:
About Question enthuware.ocajp.i.v7.2.1308 :
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
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
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1308 :
That is exactly the reason.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 42
- Joined: Mon Jan 20, 2014 2:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1308 :
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.
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.
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1308 :
toString is invoked because of: System.out.println(this);
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Thu Jul 31, 2014 11:01 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1308 :
Hi,
When i run the code like this (as suggested in your exercise):
it prints the hashcode.
but when i do like this :
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;
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;
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1308 :
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.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 2
- Joined: Thu Jul 31, 2014 11:01 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1308 :
Hi paul,
You are right.
I'm ashamed.
My flat excuses.
You are right.
I'm ashamed.
My flat excuses.
Re: About Question enthuware.ocajp.i.v7.2.1308 :
Is(.toString()) it always invoked when System.out.println() method is used?admin wrote:toString is invoked because of: System.out.println(this);
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1308 :
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)
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.
-
- Posts: 66
- Joined: Mon Feb 20, 2017 12:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1308 :
Hi Paul!
I have one doubt about this test:
what does it mean the keyword "this" inside the code:
is it a reference to the static variable is?? or is a reference to the object of new TestClass()??
Thank you very much 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;
}
}
Code: Select all
public TestClass(){
System.out.println(this);
}
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
Reason: Please enter code inside [code] [/code] tags
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1308 :
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.
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.
-
- Posts: 66
- Joined: Mon Feb 20, 2017 12:31 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1308 :
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!!
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!!
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1308 :
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.
"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.
-
- Posts: 26
- Joined: Wed Oct 02, 2019 1:42 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1308 :
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.
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1308 :
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.
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.
-
- Posts: 26
- Joined: Wed Oct 02, 2019 1:42 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1308 :
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.
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.
-
- Posts: 7
- Joined: Sat Sep 04, 2021 8:17 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1308 :
I am trying to understand that System.out.println(this);will call toString method so return"TestClass.si = "+ this.si;
I also tried changing to and to . 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 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?
I also tried changing
Code: Select all
System.out.println(this);
Code: Select all
System.out.println(si);
Code: Select all
System.out.println(TestClass.si);
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()
in this case because 'this' is passed, toString will be called so print TestClass.si = 10 . Did I understand correctly?
-
- Site Admin
- Posts: 10078
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1308 :
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.
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.
Who is online
Users browsing this forum: No registered users and 32 guests