• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question #6 from Doug's book

 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does the follwoing code print to the console:
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oooo I almost got it right (I ran the code to check)... I'lll let some other people ponder over it before I spoil the surprise.
So... once you figure out what happens... the big question is WHY does it do that?
[ August 18, 2003: Message edited by: Jessica Sant ]
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cannot resist giving a clue: within a constructor "this" point to the object being constructed.
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all
I guess I'm not eligible for giving the answer as I am not preparing for the SCJP. I'm already SCJP2 and know the answer w/o running the code. Though I will confirm it.
Regards
Maulin.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm also SCJP2, but this is still a very intriguing piece of code. I got it wrong, but after reading Jose's comment I now realize my mistake. Good example!
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm, i wonder why it spits out null,
i thought the instance variable for da object was set before the constructor executed ?
 
Shafkat Talli
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
by the way, both the classes have default access, so there will be no output, cause there is no public class. I ran it and it didnt show anything cause of that.
what do u ppl say ? but if i make the one class with main, public, i dont understand the output of null string.
[ August 19, 2003: Message edited by: Shafkat Talli ]
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Originally posted by Shafkat Talli:

hmm, i wonder why it spits out null,
i thought the instance variable for da object was set before the constructor executed ?


Every constructor defined in a class, an <init> method is created. This <init> method may contain (in the following order): calls to the superclass constructor, the instance initializers & member variable initializer(depending on their position in the code), and the remaining codes in the constructor.
So when Superclass calls the overidden() method of ABC, the variable s has not been initialized.

Line 1 is the call to the Superclass constructor. Lines 5 - 7 is the initialization of the variable s. As you can see, the initialization happened after the invocation of the Superclass constructor.
If we add the lines 1, 2, & 3 to class ABC as follows:

then the the code in the initializer will be first (line 1), followed by the assignment in line 2, and lastly by the assignment in the constructor. All of this will still happen after the call to the Superclass <init> method.
If the position of lines 1, 2 and 3 were changed, then the order of the initializer and member assignment may be affected, but not the assignment in the constructor.
Any correction on this?
[ August 19, 2003: Message edited by: Alton Hernandez ]
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would the behaviour change if the SuperClass was declared abstract. Lets say it had an abstarct method also in its definition?
Rgds,
Anupreet
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shafkat Talli:
by the way, both the classes have default access, so there will be no output, cause there is no public class. I ran it and it didnt show anything cause of that.


It works fine for me. What version of Java are using?
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Anupreet Arora:
Would the behaviour change if the SuperClass was declared abstract. Lets say it had an abstarct method also in its definition?


That would have no effect on the result.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Alton is correct. Now change the definition of "s" in ABC to make it a final variable. Will you get the same result?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another clue. s is a compile-time constant.
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are 2 different ways of implementing s as a final variable.

I find it interesting to see this because it shows 2 different ways the final variable is transformed in the code - one, like a literal, the other, like an immutable variable.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason that making "s" final prevents it from being seen as null by the super constructor is that the JLS guarantees that final variables are never seen with their default value! "s" has to be initialized or the JLS will have been violated.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I gone through the every possibility.... I conclude that Alton Hernandez is very nearer to the answer...
I somewhat modify the code ...

Answer :

I can say that before coming out from the super constructor it doesn't know the value assign to the s.But it create the space for the String s at compile time.
will it helpful ??
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Sachin.
The compiler never creates an object. It just produces the bytecodes that will do the work at runtime.
_________________________________________________________________________
The example with the blank final still prints null.
A final field initialized with a compile-time constant (JLS 15.28), however, prints its value because the compiler is able to replace each occurance of such field in the program with its proper value. After all, the value is known at compile time, and it is not going to change.
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jose Botella:

The example with the blank final still prints null.

You are correct. I didn't get that quite right. The JLS says, "�such constant fields must always appear to have been initialized; the default initial value for the type of such a field must never be observed." So it applies only to inline constants which are finals variables that are initialized with a constant value.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/* I am still not able to understand why it prints null.
Here what I thought ( I know somewhere I am going wrong, but don't know where!)
constructor is called with new operator. //1
Prints Superclass
overidden of Superclass will be called(?) //2
to print "Superclass". //2a
then "over ridden" method of "ABC" will be called. //4
and print "ABC". //5
not able to understand why "null" is printed.
If I change method name in Superclass to something else say override then null is not printed.
Please explain where I am wrong. */
import java.util.*;
class Superclass {
Superclass() {
System.out.println("Superclass");
overridden(); //1
System.out.println("Exit super");
}
void overridden() { //2
System.out.println("Superclass"); //2a
}
}

class ABC extends Superclass {
String s = "ABC";
public static void main(String[] args) {
System.out.println("1");
ABC abc = new ABC();
abc.overridden(); //4
System.out.println("2");
}
void overridden() { //5
System.out.println("3");
System.out.println(s);
System.out.println("4");
}
}
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) new operator is run on ABC
2) s is pushed onto the stack and set to its default value of null
3) constructor of Superclass is invoked
4) constructor invokes overridden()
5) overridden() has been overridden so overridden() of ABC is run
4) s has not been initialized yet so "null" prints
5) a new String is added to the String pool and s is set to point to it
6) default constructor of ABC runs
7) overridden() method of ABC runs again - this time s has been initialized
What is the result of running this code:

[ August 21, 2003: Message edited by: Thomas Paul ]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic