Results 1 to 5 of 5
Thread: Constructor Not Used ?
- 12-26-2012, 01:44 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 76
- Rep Power
- 0
Constructor Not Used ?
Consider the following Code:
class Node
{
int iData; // data used as key value
double fData; // other data
node leftChild; // this node’s left child
node rightChild; // this node’s right child
public void displayNode()
{
// (see Listing 8.1 for method body)
}
}
class Tree
{
private Node root; // the only data field in Tree
public void find(int key)
{
}
public void insert(int id, double dd)
{
}
public void delete(int id)
{
}
// various other methods
} // end class Tree
class TreeApp
{
public static void main(String[] args)
{
Tree theTree = new Tree; // make a tree
theTree.insert(50, 1.5); // insert 3 nodes
theTree.insert(25, 1.7);
theTree.insert(75, 1.9);
node found = theTree.find(25); // find node with key 25
if(found != null)
System.out.println(“Found the node with key 25”);
else
System.out.println(“Could not find node with key 25”);
} // end main()
} // end class TreeApp
In the above class, TreeApp, why the constructor with no arguments is not called while creating the object of the "Tree" class using the following statement:
Tree theTree = new Tree;
Shouldn't it be like the following?:
Tree theTree = new Tree();
Please state the reason.
Thanks
- 12-26-2012, 01:53 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Re: Constructor Not Used ?
Yep, there are parentheses needed there if you want to call the no-arg constructor. That book isn't even worth the trees it's made from ...
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-26-2012, 02:00 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 76
- Rep Power
- 0
Re: Constructor Not Used ?
Any other Java book recommended for Algorithms?
- 12-26-2012, 03:38 PM #4
Re: Constructor Not Used ?
Don't double post the same question. See the moderator remarks in Reference !
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-27-2012, 10:54 PM #5
Member
- Join Date
- Dec 2012
- Posts
- 74
- Rep Power
- 0
Re: Constructor Not Used ?
Yes, when you call a constructor, you will always have parentheses.
Also, I found some other problems with the code that you posted:
1. Some places have "node" with a lower-case "n" that should be "Node" with an upper-case "N". "Node" is the class name which begins with an upper-case "N".
2. There were some curly-quotation marks that need to be straight-quotation marks:
System.out.println("Found the node with key 25"); // I fixed this line of code by changing the curly quotes to straight quotes.
3. The method, find() should return a Node rather than having "void" as the return type.
Similar Threads
-
call default constructor inside of parameterized constructor after conditional check
By Googol in forum New To JavaReplies: 5Last Post: 08-11-2012, 09:50 AM -
Java - Constructor Method versus Constructor
By brocksoffice in forum New To JavaReplies: 1Last Post: 08-01-2012, 09:17 AM -
How to call a master constructor from a minor constructor?
By b.m in forum New To JavaReplies: 5Last Post: 12-14-2011, 01:47 PM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks