• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question from Learning Java in 21 Days  RSS feed

 
Russell Sher
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi - I am working through the Book Java in 21 Days and have a question based on some code (which I changed a bit)

The code is here, with resultant output below that- and my questions are below that:
(Thanks in advance)


The output is:

I am here
x is 0, y is 1
I am an instance of the class class SubPrinter
x is 0, y is 1, z = 3
I am an instance of the class class SubPrinter
My questions are:
1. Why WON'T:  this.getClass().getName() result in:  I am an instance of the class Printer (the superclass name)? (I only get SubPrinter as the result) How would I get that to be the result of the statement?
2. In the book an example of calling the original method (i.e. not the subclass method) is given as:

Now isn't his recursive? (i.e. the method calling itself within the method?)
I did temporarily try this in my code by placing super.printMe(); into the printMe() method (and it did work, although is this ok to do?)
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Interesting problem . You are sure that is from the super.printMe() call?

What you have when you call that method is one object; part of it is constructed from the superclass and part from the subclass. You don't have two objects, but one. Nowhere did you write new Printer(...), so you don't have a separate Printer object. So when you write this, there is only one object for it to refer to, one created from SubPrinter. So when you call getClass(), it returns a reference to a Class<SubPrinter> object.
 
Russell Sher
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply - that makes sense. I seemed to have solved it with:


That gives the output:
I am an instance of the class SubPrinter
The Superclass is class Printer
 
Knute Snortum
Sheriff
Posts: 4288
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A note about your subclass: if you format it properly, you will notice something...
The line System.out.println("I am here"); is not in a method, but in something called an instance initializer.  Is that what you intended?  Printing, "I am here" is confusing, because this code will be executed right after the class is instantiated, before main() is run.  Instance initializers should be put near the top of the code, before any methods or constructors.
 
Russell Sher
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi - thanks. Yes I was just 'experimenting' with a code block. To see the effect. So as I understand it, an unnamed block which is not part of a method will just execute when the program runs.
Is this an 'acceptable' means of doing a startup action - such as a 'Splash Screen' ? - or is it bad practice? Should code always be in a method?
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Russell Sher wrote:Hi - thanks.
That's a pleasure
Yes I was just 'experimenting'
I think just experimenting is a very good way to learn. Good for you.
. . . an unnamed block which is not part of a method . . .
That isn't just a block; that is an initialiser. Initialisers come in two kinds: instance and static. The difference in what you write is very simple:-Lines 3‑6 = instance initialiser; lines 8‑11 = static initialiser. If you look in the Java® Language Specification (=JLS) (two links above) you find that static initialisers are executed when the class is initialised and instance initialisers whenever the class is instantiated. Instance initialisers run when an object is created; the complete order is shown in this JLS chapter. Imagine that whatever you write in your instance initialiser is copied into every constructor after the super(...);/this(...); call and before any other code. I prefer constructors because you can pass arguments to constructors; you can't pass arguments to initialisers. So whatever is written in an initialiser is initialised to the same value for all instances.
On the other hand, you probably don't want to instantiate static fields in a constructor. You want them to have values before any instances exist. Als you might change their value every time a new object is created. So I think static initialisers are A Good Thing (as Sellars and Yeatman would say). Any static fields requiring anything more complicated than
private static Foo f = ...;
are probably best initialised in a static initialiser.

You know it doesn't matter which order you write methods in, or constructors, or declare fields? Beware with initialisers; if you write multile initialsers, it does matter which order you declare them in.
 
Russell Sher
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help.
Russell
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a pleasure When I wrote Als, that shou‍ld read Also.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!