Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!

Sergey Smachek

Greenhorn
+ Follow
since May 06, 2018
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
1
Received in last 30 days
0
Total given
2
Given in last 30 days
2
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Sergey Smachek

Junilu Lacar wrote:
After looking over that section and reading this paragraph several times, my opinion is that this is a poorly constructed/edited paragraph with a few disparate thoughts running into each other incoherently. The first sentence refers to line 22, the print statement that causes the output of "Inside Bath()". The rest of the paragraph is just an incoherent, confusing, and long run-on sentence. The code example it refers to demonstrates the four ways he listed that an object reference can be initialized. Only the first way, initializing at the point of definition, guarantees that the reference will be non-null (unless you explicitly initialize it to null, of course).



Thank you for answer and excellent explanation!
I had my suspicions that this was an inaccurate description in the book. But I doubted it, because I don't speak enough English.
1 week ago
Hello.
Can someone explain me this sentences from the book "Thinking in Java Bruce Eckel 4th ed."
Page 167. In section with explaining "Composition syntax"

What mean "Note that in the Bath constructor, a statement is executed before any of the initializations take place."?
Can you also explain phrase "before you send a message to an object reference"?

Primitives that are fields in a class are automatically initialized to zero, as noted in the Everything Is an Object chapter. But the object references are initialized to null, and if you try to call methods for any of them, you’ll get an exception-a runtime error. Conveniently, you can still print a null reference without throwing an exception.
It makes sense that the compiler doesn’t just create a default object for every reference, because that would incur unnecessary overhead in many cases. If you want the references initialized, you can do it:
1.
At the point the objects are defined. This means that they’ll always be initialized before the constructor is called.
2.
In the constructor for that class.
3.
Right before you actually need to use the object. This is often called lazy initialization. It can reduce overhead in situations where object creation is expensive and the object doesn’t need to be created every time.
4.
Using instance initialization.



All four approaches are shown here:


Explanation from book.

   

Note that in the Bath constructor, a statement is executed before any of the initializations take place. When you don’t initialize at the point of definition, there’s still no guarantee that you’ll perform any initialization before you send a message to an object reference—except for the inevitable run-time exception.
   When toString( ) is called it fills in s4 so that all the fields are properly initialized by the time they are used.




1 week ago
Maybe this solve you issue

1 year ago