• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

First time using two classes!

 
Darragh Bourke
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am just a beginner with programming and it's my first time using two classes and it just won't compile can someone help please?

import javax.swing.JOptionPane;

class person{
String name;
int age;
public void person(String n, int a){

name = n;
age = a ;
}
public String getDetails(){
return name +" ("+age+")";
}
}
public class sortperson
{
public static void main(String[] args)
{
String m = JOptionPane.showInputDialog(null, "Enter a Name: ");
String s = JOptionPane.showInputDialog(null, "Enter an Age: ");
int g = Integer.parseInt(s);
person p = new person(m,g);
JOptionPane.showMessageDialog(null, "Your Person is" +p);
System.exit(0);
}
}
sortperson.java:22: cannot find symbol
symbol : constructor person(java.lang.String,int)
location: class person
person p = new person(m,g);
^
1 error
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey darragh,

You have declared a return type (void) to the constructor person. The rule for constructor is, constructor can't have any return type.Not even void.
 
Darragh Bourke
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It compiled!!! Thank you Vishal Pandya.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic