Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Whats wrong with this code? Simple problem  RSS feed

 
Eddie Lee
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class homer {

public static void homer(int n)
{
System.out.print(n);

System.out.print("," + n);
}

public static void main(String[] args)
{
int num = Integer.parseInt(args[0]);
new homer(num);
}

}


I get error:

==============================

C:\Homer.java:15: cannot resolve symbol
symbol : constructor homer (int)
location: class homer
new homer(num);
^
1 error

Tool completed with exit code 1

===============================
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've not declared any constructors in your code. What you did declare was a static method that returns void. You have two options:

Change your method to a constructor by making it:

public homer(int n){....

or change the way you call it:

homer.homer(num);

(Given what it does, the latter option makes the most sense.) Also, just as a convention, class names begin with a capital letter; that is, "Homer", not "homer." And, to avoid confusion, I would change the name of the method to something other than the class name.
 
Adisesha Rao Raya
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are trying to call constructor with new homer(num) in the main().
But constructor should not contain return type and static.
so modify the class as below

class homer
{
public homer(int n)
{
System.out.print(n);

System.out.print("," + n);
}

public static void main(String[] args)
{
int num = Integer.parseInt(args[0]);
new homer(num);
}
}
 
Eddie Lee
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, please look into the following: Code Conventions for the Java� Programming Language
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!