• 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

newbie question about method overloading

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic