• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

A constructor question

 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I created a program with 2 classes: Server and DataServer. They have Super and subclass relationship. When the main program executes, it will instantiate an object reference "o". Then, the constructor of DataServer will pass serverName to the parent's constructor which will printout the nameServer. However, it was not compiled as Server.java:20: cannot reference serverName before supertype constructor has been called.
What does it mean and how should I fix it?
Thanks for help.
Andrew

[This message has been edited by Cindy Glass (edited December 17, 2001).]
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Andrew,
A valid java source file can only have one public class in it. The public class, should always, be the same as the file name. In your example, you should have saved the code as Server.java. If you had made DataServer public and Server as default (no modifier), then your approach would have worked.
Regards,
Manfred.
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your main method you have this code:
Server o = new DataServer();
When you go to create the instance of DataServer, the following steps are taken:
First the arguments for the constructor are dealt with. You don't have any in your call.
Then the first line of the DataServer constructor is looked at to see if there is a call to another constructor using this(whatever) or super(whatever). In your case there is a call to super(serverName); so that is done next. If you didn't expicitely call the super, then a call to the default constructor of the super class would have been made. However, when it tries to do the super constructor it looks at the argument (serverName) but it has not yet initialized any of the variables for either class - so you get an error. Normally the super class is completely initialized, and constructed then the execution continues with the SECOND line of the sub-class constructor, which is where you are initializing the variable.
So the pattern is:
Initialize instance initializers and instance variable initializers for the super class
Execute the constructor for the super class
- etc recursively until all super class construction is complete
Initialize instance initializers and instance variable initializers for the sub class
Execute the constructor for the sub class
- etc recursively until all sub class construction is complete.
You need to move the initialization of the serverName variable to the argument of the constructor or to the call itself.
super("Customer Service");

From the Java Lanquage Specification


12.5 Creation of New Class Instances
Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure:

1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.
2. If this constructor begins with an explicit constructor invocation of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.
3. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super). Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.
4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5. (In some early implementations, the compiler incorrectly omitted the code to initialize a field if the field initializer expression was a constant expression whose value was equal to the default initialization value for its type.)
5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.

 
Andrew Parker
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will the following example be better to explain the sequence of the initializer?
public class Test extends Sub
{
public static void main(String[] args)
{
System.out.println("This message can be printed first or third");
System.out.println("\n");
Sub s = new Sub();
System.out.println("\n");
Super r = new Super();
System.out.println("\n");
Test t = new Test();
System.out.println("\n");
Super q = new Sub();
System.out.println("\n");
}
}
class Super
{
{
System.out.println("Super class instance initializer"); // 3
}
static
{
System.out.println("Super class static initializer"); // 1
}
public Super()
{
System.out.println("Super class default constructor"); // 4
}
}
class Sub extends Super
{
{
System.out.println("Sub class instance initializer"); // 5
}
static
{
System.out.println("Sub class static initializer"); // 2
}
public Sub()
{ // need no "super();" here
System.out.println("Sub class constructor"); // 6
}
}
Thanks for both of your help.
Andrew
 
It's fun to be me, and still legal in 9 states! Wanna see my tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic