• 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

Whats wrong with this code? Simple problem

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

===============================
 
Bartender
Posts: 1844
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, please look into the following: Code Conventions for the Java� Programming Language
 
Can you shoot lasers out of your eyes? Don't look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic