aspose file tools*
The moose likes Beginning Java and the fly likes newbie question about method overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "newbie question about method overloading" Watch "newbie question about method overloading" New topic
Author

newbie question about method overloading

dinesh prasad
Greenhorn

Joined: Jun 03, 2003
Posts: 27
I'm trying to re-write a piece of code from "Thinking in Java". Here's my version, with the original following:
import java.util.*;
class Rock {
Rock() { // This is the constructor
System.out.println("Creating Rock");

}
void OLoad (String s) {
System.out.println(s);
}
public class SimpleConstructor2 {

public static void main(String[] args) {
for(int i = 0; i < 5; i++) {
Rock r = new Rock(i);
r.OLoad("OVERLOADED METHOD: rock created");
}
new Rock();
}
}
}
errors:
SimpleConstructor2.java:16: cannot resolve symbol
symbol : constructor Rock (int)
location: class Rock
Rock r = new Rock(i);
^
SimpleConstructor2.java:14: inner classes cannot have static declarations
public static void main(String[] args) {
^
________________________________________________
import java.util.*;
class Tree {
int height;
Tree() {
System.out.println("Planting a seedling");
height = 0;
}
Tree(int i) {
System.out.println("Creating new Tree that is "
+ i + " feet tall");
height = i;
}
void info() {
System.out.println("Tree is " + height + " feet tall");
}
void info(String s) {
System.out.println(s + ": Tree is "
+ height + " feet tall");
}
}
public class Overloading {
static Test monitor = new Test();
public static void main(String[] args) {
for(int i = 0; i < 5; i++) {
Tree t = new Tree(i);
t.info();
t.info("overloaded method");
}
// Overloaded constructor:
new Tree();
}
}
}
[ June 03, 2003: Message edited by: dinesh prasad ]
[ June 03, 2003: Message edited by: dinesh prasad ]
[ June 03, 2003: Message edited by: dinesh prasad ]
Tim B.
Greenhorn

Joined: Feb 03, 2003
Posts: 10
Dear Dinesh,
Though I did not see what your question was exactly, I guess you want to know why your code doesn't compile. As you probably saw already you have two errors in your code.
The first problem is the line 'Rock r = new Rock(i);'. When you write this code in plain English, it states your create a new Rock object called 'r' and initiate it by calling the 'Rock' constructor method with a integer parameter called 'i'. The problem is though that you have not defined a 'Rock' constructor that takes an integer as parameter. You only defined a parameterless constructor. So you have to add something like the following code to solve this problem:
public Rock(int i)
{
System.out.println("Creating new Rock that weighs "
+ i + " pounds");
}
Your second problem is that you define a static method in your inner class. This is not allowed. If I were you, I'd forget about inner classes for now and not touch them until you've come to know the Java language a little bit better. Stick to writing one class per file, that way your code is a bit more simple to read and a little less complex.
Hope this helped,
Tim
dinesh prasad
Greenhorn

Joined: Jun 03, 2003
Posts: 27
Ok, thanks a lot for the help Tim--I've got this snippet to work. It's great to get such an expedient reply. Thanks again.
Dinesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: newbie question about method overloading
 
Similar Threads
Problem with constructors
Second order sorting using Comparable Interface
Is there away I can have two or more classes in same file?
First program by myself. Need help on one error please!
interface example